-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206. Reverse Linked List.java
43 lines (42 loc) · 1.23 KB
/
206. Reverse Linked List.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){//边界:空链表
return null;
}
if(head.next==null){//边界:单个元素
return head;
}
ListNode temp=head.next;//存储next节点
ListNode res=reverseList(temp);//反转以next为头部的链表
temp.next=head;//此时next已经在尾部,将head放入
head.next=null;//head成为尾部,head.next置空
return res;
}
/*
//迭代法
ListNode pre=null;//反转后的链表头
ListNode cur=head;//当前链表头
while(cur!=null){
ListNode next=cur.next;
if(pre==null){//初始化操作
pre=cur;
cur.next=null;
}else{//直接插在pre的头部
cur.next=pre;
pre=cur;
}
cur=next;//将cur换为之前保存好的next
}
return pre;
*/
}