forked from zfman/AlgorithmCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseListSolution.java
More file actions
42 lines (38 loc) · 1 KB
/
Copy pathReverseListSolution.java
File metadata and controls
42 lines (38 loc) · 1 KB
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
package offer;
import leetcode.common.LinkedUtils;
import leetcode.common.ListNode;
import sort.ArrayUtils;
/**
* 反转链表
*
* 输入一个链表,反转链表后,输出新链表的表头。
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class ReverseListSolution {
public ListNode ReverseList(ListNode head) {
if(head==null) return null;
//pr:指向要反转的下一个元素
//p:指向当前正在反转的元素
//q:指向上一个反转的元素
ListNode pr=head,p=head,q=null;
while (pr!=null){
pr=p.next;
p.next=q;
q=p;
p=pr;
}
head=q;
return head;
}
public static void main(String[] args){
int[] array={
1,2,3,4,5,6
};
ListNode head=LinkedUtils.arrayToLinkedList(array);
ListNode r=new ReverseListSolution().ReverseList(head);
LinkedUtils.print(r);
}
}