FrontEndCollection icon indicating copy to clipboard operation
FrontEndCollection copied to clipboard

Reverse Linked List

Open cheatsheet1999 opened this issue 4 years ago • 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;
};

cheatsheet1999 avatar Sep 08 '21 16:09 cheatsheet1999