2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d825a367417b2b2512c88
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 反转双重链接列表
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 1
|
|
|
|
|
videoUrl: ''
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: reverse-a-doubly-linked-list
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
让我们为我们的双向链表创建一个名为reverse的方法,它可以反转列表。一旦执行该方法,头部应指向前一个尾部,尾部应指向前一个头部。现在,如果我们从头到尾遍历列表,我们应该以与原始列表相反的顺序来满足节点。尝试反转空列表应返回null。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
存在DoublyLinkedList数据结构。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof DoublyLinkedList !== 'undefined') {
|
|
|
|
|
test = new DoublyLinkedList();
|
|
|
|
|
}
|
|
|
|
|
return typeof test == 'object';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
DoublyLinkedList有一个名为add的方法。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof DoublyLinkedList !== 'undefined') {
|
|
|
|
|
test = new DoublyLinkedList();
|
|
|
|
|
}
|
|
|
|
|
if (test.add == undefined) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return typeof test.add == 'function';
|
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
DoublyLinkedList有一个名为reverse的方法。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof DoublyLinkedList !== 'undefined') {
|
|
|
|
|
test = new DoublyLinkedList();
|
|
|
|
|
}
|
|
|
|
|
if (test.reverse == undefined) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return typeof test.reverse == 'function';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
反转空列表将返回null。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof DoublyLinkedList !== 'undefined') {
|
|
|
|
|
test = new DoublyLinkedList();
|
|
|
|
|
}
|
|
|
|
|
return test.reverse() == null;
|
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
反向方法反转列表。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof DoublyLinkedList !== 'undefined') {
|
|
|
|
|
test = new DoublyLinkedList();
|
|
|
|
|
}
|
|
|
|
|
test.add(58);
|
|
|
|
|
test.add(61);
|
|
|
|
|
test.add(32);
|
|
|
|
|
test.reverse();
|
|
|
|
|
return test.print().join('') == '326158';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
当列表反转时,正确维护下一个和上一个引用。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof DoublyLinkedList !== 'undefined') {
|
|
|
|
|
test = new DoublyLinkedList();
|
|
|
|
|
}
|
|
|
|
|
test.add(11);
|
|
|
|
|
test.add(22);
|
|
|
|
|
test.add(33);
|
|
|
|
|
test.reverse();
|
|
|
|
|
return test.printReverse().join('') == '112233';
|
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
DoublyLinkedList.prototype = Object.assign(
|
|
|
|
|
DoublyLinkedList.prototype,
|
|
|
|
|
{
|
|
|
|
|
add(data) {
|
|
|
|
|
if (this.head == null) {
|
|
|
|
|
this.head = new Node(data, null);
|
|
|
|
|
this.tail = this.head;
|
|
|
|
|
} else {
|
|
|
|
|
var node = this.head;
|
|
|
|
|
var prev = null;
|
|
|
|
|
while (node.next != null) {
|
|
|
|
|
prev = node;
|
|
|
|
|
node = node.next;
|
|
|
|
|
};
|
|
|
|
|
var newNode = new Node(data, node);
|
|
|
|
|
node.next = newNode;
|
|
|
|
|
this.tail = newNode;
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
print() {
|
|
|
|
|
if (this.head == null) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
var result = new Array();
|
|
|
|
|
var node = this.head;
|
|
|
|
|
while (node.next != null) {
|
|
|
|
|
result.push(node.data);
|
|
|
|
|
node = node.next;
|
|
|
|
|
};
|
|
|
|
|
result.push(node.data);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
printReverse() {
|
|
|
|
|
if (this.tail == null) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
var result = new Array();
|
|
|
|
|
var node = this.tail;
|
|
|
|
|
while (node.prev != null) {
|
|
|
|
|
result.push(node.data);
|
|
|
|
|
node = node.prev;
|
|
|
|
|
};
|
|
|
|
|
result.push(node.data);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var Node = function(data, prev) {
|
|
|
|
|
this.data = data;
|
|
|
|
|
this.prev = prev;
|
|
|
|
|
this.next = null;
|
|
|
|
|
};
|
|
|
|
|
var DoublyLinkedList = function() {
|
|
|
|
|
this.head = null;
|
|
|
|
|
this.tail = null;
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
var Node = function(data, prev) {
|
|
|
|
|
this.data = data;
|
|
|
|
|
this.prev = prev;
|
|
|
|
|
this.next = null;
|
|
|
|
|
};
|
|
|
|
|
var DoublyLinkedList = function() {
|
|
|
|
|
this.head = null;
|
|
|
|
|
this.tail = null;
|
|
|
|
|
|
|
|
|
|
this.reverse = function() {
|
|
|
|
|
if (!this.head || !this.head.next) {
|
|
|
|
|
return this.head
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let tail;
|
|
|
|
|
let temp;
|
|
|
|
|
let current = this.head;
|
|
|
|
|
while(current !== null) {
|
|
|
|
|
if(!tail) tail = current;
|
|
|
|
|
temp = current.prev;
|
|
|
|
|
current.prev = current.next;
|
|
|
|
|
current.next = temp;
|
|
|
|
|
current = current.prev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.head = temp.prev;
|
|
|
|
|
this.tail = tail
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|