-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
89 lines (81 loc) · 2.34 KB
/
Solution.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ds.reverse.leetcode206;
/**
* 反转链表
* LeetCode 206 https://leetcode-cn.com/problems/reverse-linked-list/
*
* @author yangyi 2021年01月19日11:02:03
*/
public class Solution {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
private ListNode createLink() {
ListNode node_1 = new ListNode(1);
ListNode node_2 = new ListNode(2);
ListNode node_3 = new ListNode(3);
ListNode node_4 = new ListNode(4);
ListNode node_5 = new ListNode(5);
node_1.next = node_2;
node_2.next = node_3;
node_3.next = node_4;
node_4.next = node_5;
return node_1;
}
private void printLink(ListNode head) {
for (ListNode cur = head; cur != null; cur = cur.next) {
System.out.print(cur.val + " ——> ");
}
}
/**
* 用迭代实现反转链表
*/
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return head;
}
ListNode pre = null, cur = head, next;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
/**
* 用递归实现反转链表
*/
public ListNode reverseListRecursive(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return head;
}
ListNode last = reverseListRecursive(head.next);
head.next.next = head;
head.next = null;
return last;
}
public static void main(String[] args) {
Solution reverseLink = new Solution();
ListNode head = reverseLink.createLink();
System.out.println("遍历输出创建好的链表:");
reverseLink.printLink(head);
System.out.println();
System.out.println("采用迭代方法反转后的单链表:");
ListNode result = reverseLink.reverseList(head);
reverseLink.printLink(result);
System.out.println();
System.out.println("采用递归方法反转后的单链表");
ListNode result2 = reverseLink.reverseListRecursive(result);
reverseLink.printLink(result2);
}
}