2019-02-26 algorithm leetcode -reverseLinkedList Leetcode题解之 —— 反转链表 思路 思路一: 转化为数组 利用reverse反转 思路二: 双指针迭代 prev指针存储上一个节点 current迭代循环 题解 数组法: 1234567891011121314151617181920212223242526/** * @param {ListNode} head * @return {ListNode} */var reverseList = function(head) { // TODO solution 1 const cache = []; let current = head; while (current) { cache.push(new ListNode(current.val)); current = current.next; } if (!cache.length) { return null; } cache.reverse(); for (const [index, node] of cache.entries()) { node.next = cache[index + 1]; } return cache[0];}; 双指针法: 1234567891011121314151617/** * @param {ListNode} head * @return {ListNode} */var reverseList = function(head) { let prev = null; let current = head; while (current) { const next = current.next; current.next = prev; prev = current; current = next; } return prev;}; 作者 : zhaoyang Duan 地址 : https://ddzy.github.io/blog/2019/02/26/leetcode-reverseLinkedList/ 来源 : https://ddzy.github.io/blog 著作权归作者所有,转载请联系作者获得授权。