Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Reverse Linked List #24

Open
Open
@cheatsheet1999

Description

@cheatsheet1999

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;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions