首页 > 编程语言 > 详细

[LeetCode][JavaScript]Reverse Linked List

时间:2015-05-30 16:28:51      阅读:216      评论:0      收藏:0      [点我收藏+]

Reverse Linked List 

Reverse a singly linked list.

click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

https://leetcode.com/problems/reverse-linked-list/

 

 


 

 

 

反转链表。

 1 /**
 2  * Definition for singly-linked list.
 3  * function ListNode(val) {
 4  *     this.val = val;
 5  *     this.next = null;
 6  * }
 7  */
 8 /**
 9  * @param {ListNode} head
10  * @return {ListNode}
11  */
12 var reverseList = function(head) {
13     if(!head || !head.next){
14         return head;
15     }
16 
17     var res = new ListNode(-1);
18     res.next = head;
19     head = head.next;
20     res.next.next = null;
21     while(head){
22         var tmp = head.next;
23         head.next = res.next;
24         res.next = head;
25         head = tmp;
26     }
27 
28     return res.next;
29 };

 

 

 

[LeetCode][JavaScript]Reverse Linked List

原文:http://www.cnblogs.com/Liok3187/p/4540490.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!