Reverse Linked List

LeetCode題目: 206. Reverse Linked List

My solution:

/**
 *  
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
let reverseList = head => {
    let node = null;
    while(head !== null) {
        const temp = head.next;
        head.next = node;
        node = head;
        head = temp;
    }
    return node;    
}
profile-image
Hi, 我是 Zeki。目前為一名前端工程師。我相信科技始終來自於人性,是用來幫助人們過上更有品質的生活的,但願也希望如此。