题目链接:https://leetcode-cn.com/problems/reverse-linked-list/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let h = new ListNode();
while(head !== null){
let p = head;
head = head.next;
p.next = h.next;
h.next = p;
}
return h.next;
};

本文详细解析了如何通过迭代方式实现链表的反转,并提供了一段JavaScript代码示例。该算法适用于解决LeetCode上的反转链表题目。
259

被折叠的 条评论
为什么被折叠?



