我正在使用重新排列鏈接串列的代碼,以便以交替方式從串列的前面和后面獲取節點,因此轉換 1->2->3->4->5- 的串列>6 到 1->6->2->5->3->4。
這是就地重新排列給定鏈表的代碼:
class LinkedList {
static Node head; // head of the list
/* Node Class */
static class Node {
int data;
Node next;
// Constructor to create a new node
Node(int d)
{
data = d;
next = null;
}
}
void printlist(Node node)
{
if (node == null) {
return;
}
while (node != null) {
System.out.print(node.data " -> ");
node = node.next;
}
}
Node reverselist(Node node)
{
Node prev = null, curr = node, next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
node = prev;
return node;
}
void rearrange(Node node)
{
// 1) Find the middle point using tortoise and hare
// method
Node slow = node, fast = slow.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// 2) Split the linked list in two halves
// node1, head of first half 1 -> 2 -> 3
// node2, head of second half 4 -> 5
Node node1 = node;
Node node2 = slow.next;
slow.next = null;
// 3) Reverse the second half, i.e., 5 -> 4
node2 = reverselist(node2);
// 4) Merge alternate nodes
node = new Node(0); // Assign dummy Node
// curr is the pointer to this dummy Node, which
// will be used to form the new list
Node curr=node;
while (node1 != null || node2 != null) {
// First add the element from first list
if (node1 != null) {
curr.next = node1;
curr = curr.next;
node1 = node1.next;
}
// Then add the element from second list
if (node2 != null) {
curr.next = node2;
curr = curr.next;
node2 = node2.next;
}
}
// Assign the head of the new list to head pointer
node = node.next;
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.head.next.next.next = new Node(4);
list.head.next.next.next.next = new Node(5);
list.head.next.next.next.next.next = new Node(6);
list.printlist(head); // print original list
list.rearrange(head); // rearrange list as per ques
System.out.println("");
list.printlist(head); // print modified list
}
}
我的問題是關于這部分代碼:
Node curr=node;
while (node1 != null || node2 != null) {
// First add the element from first list
if (node1 != null) {
curr.next = node1;
curr = curr.next;
node1 = node1.next;
}
// Then add the element from second list
if (node2 != null) {
curr.next = node2;
curr = curr.next;
node2 = node2.next;
}
}
我在 VScode 中對此進行了除錯,我看到curr指向node該陳述句curr.next=node1的時間會在node. 但是在做的時候curr=curr.next不會對node. 為什么會這樣?
uj5u.com熱心網友回復:
curr是指向node并且當curr.next=node1它也在節點中創建更改但當curr=curr.next它沒有對節點進行任何更改時node
對物件進行更改的唯一方法是以某種方式分配給該物件的屬性。這就是這樣curr.next=node1做的:它將某些東西分配給物件的next屬性。
另一方面,對變數的賦值永遠不會改變物件。它只是改變變數的內容。就是curr=curr.next這樣。
它可能有助于將其可視化。假設我們node在第一個回圈之后(在一個只有一個節點的串列中)
node
↓
┌────────────┐
│ data: 1 │
│ next: null │
└────────────┘
然后Node node1 = node;創建對同一節點的新參考:
node node1
↓ ↓
┌────────────┐
│ data: 1 │
│ next: null │
└────────────┘
稍后,我們創建一個新節點node = new Node(0);:
node node1
↓ ↓
┌────────────┐ ┌────────────┐
│ data: 0 │ │ data: 1 │
│ next: null │ │ next: null │
└────────────┘ └────────────┘
執行時Node curr=node;,我們只得到一個參考相同節點的變數node:
node curr node1
↓ ↓ ↓
┌────────────┐ ┌────────────┐
│ data: 0 │ │ data: 1 │
│ next: null │ │ next: null │
└────────────┘ └────────────┘
curr.next=node1執行時,物件的屬性next設定為參考正在參考的物件node1。所以:
node curr node1
↓ ↓ ↓
┌────────────┐ ┌────────────┐
│ data: 0 │ │ data: 1 │
│ next: ────────?│ next: null │
└────────────┘ └────────────┘
但是當curr=curr.next被執行時,沒有物件被改變。這分配給一個變數,并且只影響該變數,而不是它參考的內容:
node curr node1
↓ ↓ ↓
┌────────────┐ ┌────────────┐
│ data: 0 │ │ data: 1 │
│ next: ────────?│ next: null │
└────────────┘ └────────────┘
我希望這可以澄清差異。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505874.html
上一篇:用于對齊128塊中的數字的公式
