标签:tps asc color lis ems 好的 har ase discus
这类reverse的题不会写,会写homebrew也枉然。这题我用的是iterative的思路做的。创建一个空的指针pre。当head不为空的时候,先存住head.next,然后head.next指向pre,最后pre,head,next三个指针整体往后移动一位。代码如下,也share一个注释写的比较好的discussion吧,https://leetcode.com/problems/reverse-linked-list/discuss/375450/Javascript-Iterative-with-explanationcomments
1 /** 2 * @param {ListNode} head 3 * @return {ListNode} 4 */ 5 var reverseList = function(head) { 6 // corner case 7 if (head === null || head.next === null) { 8 return head; 9 } 10 11 // normal case 12 let pre = null; 13 while (head !== null) { 14 let next = head.next; 15 head.next = pre; 16 pre = head; 17 head = next; 18 } 19 return pre; 20 };
[LeetCode] 206. Reverse Linked List
标签:tps asc color lis ems 好的 har ase discus
原文地址:https://www.cnblogs.com/aaronliu1991/p/11670754.html