LeetCode 206. Reverse Linked List

Here is a quick solution to Reverse Linked List problem.

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    let curr = head;
    let prev = null;

    while (curr) {
        let next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }

    return prev;
};
Published on

Previous post: Importing plotly failed. Interactive plots will not work.

Next post: LeetCode 92. Reverse Linked List II