cheatsheet1999/FrontEndCollection

Reverse Linked List

cheatsheet1999 opened this issue · 0 comments

Given the head of a singly linked list, reverse the list, and return the reversed list.

Screen Shot 2021-09-08 at 9 07 14 AM

reverseLinkedlist

With the help with ES6, I found this problem can be easy to solve

var reverseList = function(head) {
    let [prev, current] = [null, head];
    while(current) {
        [prev, current.next, current] = [current, prev, current.next]
    }
    return prev;
};