交换链表时出现的错误

huangapple go评论69阅读模式
英文:

Bug while Swapping a LinkedList

问题

以下是你要翻译的代码部分:

public void reversePI() {
    Node N1 = this.tail;
    Node Helper = this.head.next;
    this.tail = this head;
    this.tail.next = null;
    this.head = N1;
    this.head.next = Helper;
    this.display();
}

public void display() {
    Node current = this.head;
    while (current != null) {
        System.out.print(current.data + " ");
        current = current.next;
    }
}

请注意,代码中存在一些错误。以下是修正后的代码:

public void reversePI() {
    Node N1 = this.tail;
    Node Helper = this.head.next;
    this.tail = this.head;
    this.tail.next = null;
    this.head = N1;
    this.head.next = Helper;
    this.display();
}

public void display() {
    Node current = this.head;
    while (current != null) {
        System.out.print(current.data + " ");
        current = current.next;
    }
}

你的方法是正确的,它交换了单链表的头部和尾部。希望这有所帮助。

英文:

Here I am trying to Swap the Head and tail of a singly Linked list

 public void reversePI() {
        Node N1 = this.tail;
        Node Helper = this.head.next;
        this.tail = this.head;
        this.tail.next = null;
        this.head = N1;
        this.head.next = Helper;
        this.display();
    }

Here is the further Approach for the above function to explain my way of thinking.
Let us assume we have a Link list as

4k(adress) 1(data)-->5k(adress) 2(data)-->6k(adress) 3(data)-->7k(adress) 4(data)-->null

we have a code

now when the first line executes

N1= 7k(adress) 4(data)

Helper=5k(adress) 2(data)

this.tail=4k(adress) 1(data)

at this point the list should be

  5k(adress) 2(data)-->6k(adress) 3(data)-->4k(adress) 1(data)-->5k(adress) 2(data)

now we set the tail as null to break the initial link

this.tail.next = null;

5k(adress) 2(data)-->6k(adress) 3(data)-->4k(adress) 1(data)-->null

now this.head = N1;

7k(adress) 4(data)  5k(adress) 2(data)-->6k(adress) 3(data)-->4k(adress) 1(data)-->null

after this this.head.next = Helper; line will establish the link and

7k(adress) 4(data)--> 5k(adress) 2(data)-->6k(adress) 3(data)-->4k(adress) 1(data)-->null

is my Approch correct or is there something I am missing that leads to generating an infinite output while running the display method -->

public void display() {
            Node current = this.head;
            while (current != null) {
                System.out.print(current.data + " ");
                current = current.next;
            }
        }

答案1

得分: 2

你忘记处理倒数第二个节点的下一个节点,它应该指向新的尾节点,但它仍然指向旧的尾节点,即新的头节点。这导致了一个循环:头 -> ... -> 倒数第二个节点 -> 头

英文:

You forgot to handle the second last node's next, it should point to the new tail, but it is still pointing to the old tail which is the new head. This results in a loop: head -> ... -> secondLastNode -> head

huangapple
  • 本文由 发表于 2023年2月14日 02:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定