-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseLinkedList.java
More file actions
executable file
·55 lines (52 loc) · 1.41 KB
/
Copy pathReverseLinkedList.java
File metadata and controls
executable file
·55 lines (52 loc) · 1.41 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
43
44
45
46
47
48
49
50
51
52
53
54
55
package list;
/**
* @Author: Mr.Z
* @DateTime: 2021/01/14 17:26
* @Description: 206. 反转单链表 https://leetcode-cn.com/problems/reverse-linked-list/
* <p>
* 输入: 1->2->3->4->5->NULL
* 输出: 5->4->3->2->1->NULL
* <p>
* 进阶:
* 可以迭代或递归地反转链表。
*/
public class ReverseLinkedList {
/**
* 双指针
* @param head
* @return
*/
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode tmp = cur.next;
// 每次访问的原链表节点都会成为新链表的头结点
cur.next = pre;
// 更新新链表
pre = cur;
cur = tmp;
}
return pre;
}
/**
* 递归
*
* @param head
* @return
*/
public ListNode reverseList1(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
//从当前节点的下一个结点开始递归调用
ListNode p = reverseList(next);
//head挂到next节点的后面就完成了链表的反转。
next.next = head;
//这里head相当于变成了尾结点,尾结点都是为空的,
//否则会构成环
head.next = null;
return p;
}
}