all-of-frontend/algorithm/链表/reverseList.js
2021-04-13 21:48:01 +08:00

27 lines
537 B
JavaScript

// 递归版
var reverseList = function(head) {
const dummy = new ListNode()
return reverse(head, dummy).next
};
function reverse (head, dummy) {
if (!head) return dummy
const tmp = head.next
head.next = dummy.next
dummy.next = head
head = tmp
return reverse(head, dummy)
}
// 迭代版
var reverseList = function(head) {
const dummy = new ListNode()
while (head) {
const tmp = head.next
head.next = dummy.next
dummy.next = head
head = tmp
}
return dummy.next
};