翻转单向链表

2020-07-14  本文已影响0人  Time_Notes

var reverseList = function(head) {

    let pre = null;  //End of the reversed linked list set to null

    while(head){

        let temp = head.next; // References the next Node of given linked list

        head.next = pre; // head.next point to previous Node

        pre = head; // Move the prev Node pointer up to head

        head = temp; // Move the head up to next Node of the given linked list

    }

    return pre;

};

上一篇 下一篇

猜你喜欢

热点阅读