cheatsheet1999 / FrontEndCollection

Notes for Fullstack Software Engineers. Covers common data structure and algorithms, web concepts, Javascript / TypeScript, React, and more!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reverse Linked List

cheatsheet1999 opened this issue · 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;
};