您的位置:首页 > 其它

206. Reverse Linked List

2016-03-09 23:26 281 查看
Reverse a singly linked list.

click to show more hints.

Subscribe to see which companies asked this question

//递归
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null)return head;
ListNode tmp =head.next;
head.next=null;
ListNode rev_list=reverseList(tmp);
tmp.next=head;
return rev_list;

}
}
//非递归
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null)return head;
ListNode t1=head;
ListNode t2 =head.next;
head.next=null;
while(t2!=null){
ListNode t3=t2.next;
t2.next=t1;
t1=t2;
t2=t3;
}
return t1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: