LeetCode 反转链表

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
//第一次
var
reverseList = function(head) { if(!head) return null let per = head let next = per.next per.next = null while(next){ per = next next = per.next per.next = head head = per } return per };


//题解后
var
reverseList = function(head) { let cur = head let prev = null while(cur){ cur = cur.next head.next = prev prev = head head = cur } return prev };



原文地址:https://www.cnblogs.com/jiaobaba/p/14316037.html