Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc18d119

Browse files
jadestrongtrekhleb
authored andcommitted
fixed doubly-linked-list reverse method omit change node's previous (trekhleb#257)
1 parent4e27a97 commitc18d119

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

‎src/data-structures/doubly-linked-list/DoublyLinkedList.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,11 @@ export default class DoublyLinkedList {
243243
while(currNode){
244244
// Store next node.
245245
nextNode=currNode.next;
246+
prevNode=currNode.previous;
246247

247248
// Change next node of the current node so it would link to previous node.
248249
currNode.next=prevNode;
250+
currNode.previous=nextNode;
249251

250252
// Move prevNode and currNode nodes one step forward.
251253
prevNode=currNode;

‎src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,22 +236,43 @@ describe('DoublyLinkedList', () => {
236236
linkedList
237237
.append(1)
238238
.append(2)
239-
.append(3);
239+
.append(3)
240+
.append(4);
240241

241-
expect(linkedList.toString()).toBe('1,2,3');
242+
expect(linkedList.toString()).toBe('1,2,3,4');
242243
expect(linkedList.head.value).toBe(1);
243-
expect(linkedList.tail.value).toBe(3);
244+
expect(linkedList.tail.value).toBe(4);
244245

245246
// Reverse linked list.
246247
linkedList.reverse();
247-
expect(linkedList.toString()).toBe('3,2,1');
248-
expect(linkedList.head.value).toBe(3);
248+
expect(linkedList.toString()).toBe('4,3,2,1');
249+
250+
expect(linkedList.head.previous).toBeNull();
251+
expect(linkedList.head.value).toBe(4);
252+
expect(linkedList.head.next.value).toBe(3);
253+
expect(linkedList.head.next.next.value).toBe(2);
254+
expect(linkedList.head.next.next.next.value).toBe(1);
255+
256+
expect(linkedList.tail.next).toBeNull();
249257
expect(linkedList.tail.value).toBe(1);
258+
expect(linkedList.tail.previous.value).toBe(2);
259+
expect(linkedList.tail.previous.previous.value).toBe(3);
260+
expect(linkedList.tail.previous.previous.previous.value).toBe(4);
250261

251262
// Reverse linked list back to initial state.
252263
linkedList.reverse();
253-
expect(linkedList.toString()).toBe('1,2,3');
264+
expect(linkedList.toString()).toBe('1,2,3,4');
265+
266+
expect(linkedList.head.previous).toBeNull();
254267
expect(linkedList.head.value).toBe(1);
255-
expect(linkedList.tail.value).toBe(3);
268+
expect(linkedList.head.next.value).toBe(2);
269+
expect(linkedList.head.next.next.value).toBe(3);
270+
expect(linkedList.head.next.next.next.value).toBe(4);
271+
272+
expect(linkedList.tail.next).toBeNull();
273+
expect(linkedList.tail.value).toBe(4);
274+
expect(linkedList.tail.previous.value).toBe(3);
275+
expect(linkedList.tail.previous.previous.value).toBe(2);
276+
expect(linkedList.tail.previous.previous.previous.value).toBe(1);
256277
});
257278
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp