难以理解反转链表的工作原理
trouble understanding how reversing a linked list works
我正在反转链表。我知道这是一项微不足道的任务,但我有一个理解问题。
我在 JavaScript
中使用 class 语法
class LinkedList {
constructor(value) {
this.head = {
value: value,
next: null
};
this.tail = this.head;
this.length = 1;
}
append(value) {
const newNode = {
value: value,
next: null
}
this.tail.next = newNode;
this.tail = newNode;
this.length++;
return this;
}
reverse() {
if (!this.head.next) {
return this.head;
}
let first = this.head;
this.tail = this.head;
let second = first.next;
let temp = null
while(second) {
temp = second.next;
second.next = first;
first = second;
second = temp;
console.log('first', first)
}
this.head.next = null;
console.log('first after', first)
this.head = first;
return this
}
}
let myLinkedList = new LinkedList(5);
myLinkedList.append(1)
myLinkedList.append(99)
myLinkedList.reverse()
我无法理解的是:在 while 循环的最后一次迭代之后,first
变量应该指向这个对象(它是 console.log('first', first)
):
{ value: 99,
next: { value: 1, next: { value: 5, next: [Circular] } } }
然而,循环结束后,first
开始指向
这个,它给了我们正确的答案(它是 console.log('first after', first))
:
{ value: 99,
next: { value: 1, next: { value: 5, next: null } } }
我什至尝试画图,但仍然无法理解为什么会这样(为什么first.next.next.next
开始指向null)
这是因为 this.head.next = null;
.
行
first.next.next.next
和 this.head
指向同一个节点:5
.
以下是您的代码以及一些额外的注释:
reverse() {
// At the start of the invocation, the linked list will be:
// head: 5
// (mid): 1
// tail: 99
if (!this.head.next) {
return this.head;
}
let first = this.head;
this.tail = this.head;
let second = first.next;
let temp = null
while(second) {
temp = second.next;
second.next = first;
first = second;
second = temp;
console.log('first', first)
}
// At this point, the `next` pointer of each node is updated together
// with `this.tail`, but `this.head` still refers to the previous head (now tail).
// `this.head` and `first.next.next.next` reference the same node '5'.
this.head.next = null;
console.log('first after', first)
// Only after this assignment will the head be updated, resulting in the following linked list:
// head: 99
// (mid): 1
// tail: 5
this.head = first;
return this
}
我正在反转链表。我知道这是一项微不足道的任务,但我有一个理解问题。 我在 JavaScript
中使用 class 语法class LinkedList {
constructor(value) {
this.head = {
value: value,
next: null
};
this.tail = this.head;
this.length = 1;
}
append(value) {
const newNode = {
value: value,
next: null
}
this.tail.next = newNode;
this.tail = newNode;
this.length++;
return this;
}
reverse() {
if (!this.head.next) {
return this.head;
}
let first = this.head;
this.tail = this.head;
let second = first.next;
let temp = null
while(second) {
temp = second.next;
second.next = first;
first = second;
second = temp;
console.log('first', first)
}
this.head.next = null;
console.log('first after', first)
this.head = first;
return this
}
}
let myLinkedList = new LinkedList(5);
myLinkedList.append(1)
myLinkedList.append(99)
myLinkedList.reverse()
我无法理解的是:在 while 循环的最后一次迭代之后,first
变量应该指向这个对象(它是 console.log('first', first)
):
{ value: 99,
next: { value: 1, next: { value: 5, next: [Circular] } } }
然而,循环结束后,first
开始指向
这个,它给了我们正确的答案(它是 console.log('first after', first))
:
{ value: 99,
next: { value: 1, next: { value: 5, next: null } } }
我什至尝试画图,但仍然无法理解为什么会这样(为什么first.next.next.next
开始指向null)
这是因为 this.head.next = null;
.
行
first.next.next.next
和 this.head
指向同一个节点:5
.
以下是您的代码以及一些额外的注释:
reverse() {
// At the start of the invocation, the linked list will be:
// head: 5
// (mid): 1
// tail: 99
if (!this.head.next) {
return this.head;
}
let first = this.head;
this.tail = this.head;
let second = first.next;
let temp = null
while(second) {
temp = second.next;
second.next = first;
first = second;
second = temp;
console.log('first', first)
}
// At this point, the `next` pointer of each node is updated together
// with `this.tail`, but `this.head` still refers to the previous head (now tail).
// `this.head` and `first.next.next.next` reference the same node '5'.
this.head.next = null;
console.log('first after', first)
// Only after this assignment will the head be updated, resulting in the following linked list:
// head: 99
// (mid): 1
// tail: 5
this.head = first;
return this
}