剑指offer 反转链表

题目详情

输入一个链表,反转链表后,输出新链表的表头。

代码

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
/*
public class ListNode {
int val;
ListNode next = null;

ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null){
return null;
}
ListNode res = head;
if(head.next == null){
return res;
}else{
head = head.next;
res.next = null;
while(head != null){
ListNode temp = head;
head = head.next;
temp.next = res;
res = temp;
}
}
return res;


}
}