非递归:
function reverseList(head) {
var prev = null;
while (head) {
var next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
递归:
function reverseList(head) {
if (!head || !head.next) {
return head;
}
var newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
leetcode206-Reverse Linked List-反转链表
最新推荐文章于 2023-05-18 11:08:33 发布