Skip to content

Commit 370848d

Browse files
authored
Simplify linked-list reverse method
1 parent 15f784e commit 370848d

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/data-structures/linked-list.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,19 @@
258258
if (!this.first || !this.first.next) {
259259
return;
260260
}
261-
var current = this.first.next;
262-
var prev = this.first;
263-
var temp;
264-
while (current) {
265-
temp = current.next;
266-
current.next = prev;
267-
prev.prev = current;
268-
prev = current;
269-
current = temp;
270-
}
271-
this.first.next = null;
272-
this.last.prev = null;
273-
temp = this.first;
274-
this.first = prev;
275-
this.last = temp;
261+
var current = this.first
262+
var next
263+
264+
do {
265+
next = current.next
266+
current.next = current.prev
267+
current.prev = next
268+
current = next
269+
} while (next)
270+
271+
var tmp = this.first
272+
this.first = this.last
273+
this.last = tmp
276274
};
277275

278276
})(typeof window === 'undefined' ? module.exports : window);

0 commit comments

Comments
 (0)