您的位置:首页 > 其它

leetcode206-Reverse Linked List-反转链表

2018-02-08 21:43 651 查看
非递归:
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: