您的位置:首页 > 其它

LeetCode-206. Reverse Linked List

2017-06-27 21:26 309 查看

Reverse Linked List

Reverse a singly linked list.

/**
* Definition for singly-linked list.
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
if(head===null) return null;
if(head.next===null) return head;
var p=head.next;
var q=reverseList(p);
head.next=null;
p.next=head;
return q;
};


/**
* Definition for singly-linked list.
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
if(head===null||head.next===null) return head;
var p=head;
var q=head.next;
p.next=null;
var tmp;
while(q!==null){
tmp=q.next;
q.next=p;
p=q;
q=tmp;
}
return p;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: