- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution206.java
More file actions
Latest commit
executable file
·20 lines (18 loc) · 830 Bytes
/
Copy pathSolution206.java
File metadata and controls
executable file
·20 lines (18 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
publicclassSolution206 {
publicListNodereverseList(ListNodehead) {
// 左侧为了放在head为null,右侧为递归跳出条件
if(head == null||head.next == null){
returnhead;
}
// 对于1-2-3-4-5来说,当递归执行到4-5-null的时候,进行reverseList(5-null)判断会返回5-null不变,则跳过了改行进行4-5这个处理
ListNodereversedList = reverseList(head.next);
// 进行反推,把4-5-null中的4和5之间进行断开,tmp=5-null
ListNodetmp = head.next;
// tmp = 5-4-5-nul,此处head还是4-5-null
tmp.next = head;
// 把4后断开,tmp即为5-4-null
head.next = null;
// 因为是直接在地址上进行修改,所以直接返回即可
returnreversedList;
}
}