Skip to content

Commit 688e979

Browse files
committed
add public 04
1 parent 500c926 commit 688e979

6 files changed

+370
-2
lines changed

src/class04/Code01_ReverseList.java

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package class04;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Code01_ReverseList {
7+
8+
public static class Node {
9+
public int value;
10+
public Node next;
11+
12+
public Node(int data) {
13+
value = data;
14+
}
15+
}
16+
17+
public static class DoubleNode {
18+
public int value;
19+
public DoubleNode last;
20+
public DoubleNode next;
21+
22+
public DoubleNode(int data) {
23+
value = data;
24+
}
25+
}
26+
27+
public static Node reverseLinkedList(Node head) {
28+
Node pre = null;
29+
Node next = null;
30+
while (head != null) {
31+
next = head.next;
32+
head.next = pre;
33+
pre = head;
34+
head = next;
35+
}
36+
return pre;
37+
}
38+
39+
public static DoubleNode reverseDoubleList(DoubleNode head) {
40+
DoubleNode pre = null;
41+
DoubleNode next = null;
42+
while (head != null) {
43+
next = head.next;
44+
head.next = pre;
45+
head.last = next;
46+
pre = head;
47+
head = next;
48+
}
49+
return pre;
50+
}
51+
52+
public static Node testReverseLinkedList(Node head) {
53+
if (head == null) {
54+
return null;
55+
}
56+
ArrayList<Node> list = new ArrayList<>();
57+
while (head != null) {
58+
list.add(head);
59+
head = head.next;
60+
}
61+
list.get(0).next = null;
62+
int N = list.size();
63+
for (int i = 1; i < N; i++) {
64+
list.get(i).next = list.get(i - 1);
65+
}
66+
return list.get(N - 1);
67+
}
68+
69+
public static DoubleNode testReverseDoubleList(DoubleNode head) {
70+
if (head == null) {
71+
return null;
72+
}
73+
ArrayList<DoubleNode> list = new ArrayList<>();
74+
while (head != null) {
75+
list.add(head);
76+
head = head.next;
77+
}
78+
list.get(0).next = null;
79+
DoubleNode pre = list.get(0);
80+
int N = list.size();
81+
for (int i = 1; i < N; i++) {
82+
DoubleNode cur = list.get(i);
83+
cur.last = null;
84+
cur.next = pre;
85+
pre.last = cur;
86+
pre = cur;
87+
}
88+
return list.get(N - 1);
89+
}
90+
91+
// for test
92+
public static Node generateRandomLinkedList(int len, int value) {
93+
int size = (int) (Math.random() * (len + 1));
94+
if (size == 0) {
95+
return null;
96+
}
97+
size--;
98+
Node head = new Node((int) (Math.random() * (value + 1)));
99+
Node pre = head;
100+
while (size != 0) {
101+
Node cur = new Node((int) (Math.random() * (value + 1)));
102+
pre.next = cur;
103+
pre = cur;
104+
size--;
105+
}
106+
return head;
107+
}
108+
109+
// for test
110+
public static DoubleNode generateRandomDoubleList(int len, int value) {
111+
int size = (int) (Math.random() * (len + 1));
112+
if (size == 0) {
113+
return null;
114+
}
115+
size--;
116+
DoubleNode head = new DoubleNode((int) (Math.random() * (value + 1)));
117+
DoubleNode pre = head;
118+
while (size != 0) {
119+
DoubleNode cur = new DoubleNode((int) (Math.random() * (value + 1)));
120+
pre.next = cur;
121+
cur.last = pre;
122+
pre = cur;
123+
size--;
124+
}
125+
return head;
126+
}
127+
128+
// for test
129+
public static List<Integer> getLinkedListOriginOrder(Node head) {
130+
List<Integer> ans = new ArrayList<>();
131+
while (head != null) {
132+
ans.add(head.value);
133+
head = head.next;
134+
}
135+
return ans;
136+
}
137+
138+
// for test
139+
public static boolean checkLinkedListReverse(List<Integer> origin, Node head) {
140+
for (int i = origin.size() - 1; i >= 0; i--) {
141+
if (!origin.get(i).equals(head.value)) {
142+
return false;
143+
}
144+
head = head.next;
145+
}
146+
return true;
147+
}
148+
149+
// for test
150+
public static List<Integer> getDoubleListOriginOrder(DoubleNode head) {
151+
List<Integer> ans = new ArrayList<>();
152+
while (head != null) {
153+
ans.add(head.value);
154+
head = head.next;
155+
}
156+
return ans;
157+
}
158+
159+
// for test
160+
public static boolean checkDoubleListReverse(List<Integer> origin, DoubleNode head) {
161+
DoubleNode end = null;
162+
for (int i = origin.size() - 1; i >= 0; i--) {
163+
if (!origin.get(i).equals(head.value)) {
164+
return false;
165+
}
166+
end = head;
167+
head = head.next;
168+
}
169+
for (int i = 0; i < origin.size(); i++) {
170+
if (!origin.get(i).equals(end.value)) {
171+
return false;
172+
}
173+
end = end.last;
174+
}
175+
return true;
176+
}
177+
178+
// for test
179+
public static void main(String[] args) {
180+
int len = 50;
181+
int value = 100;
182+
int testTime = 100000;
183+
System.out.println("test begin!");
184+
for (int i = 0; i < testTime; i++) {
185+
Node node1 = generateRandomLinkedList(len, value);
186+
List<Integer> list1 = getLinkedListOriginOrder(node1);
187+
node1 = reverseLinkedList(node1);
188+
if (!checkLinkedListReverse(list1, node1)) {
189+
System.out.println("Oops1!");
190+
}
191+
192+
Node node2 = generateRandomLinkedList(len, value);
193+
List<Integer> list2 = getLinkedListOriginOrder(node2);
194+
node2 = testReverseLinkedList(node2);
195+
if (!checkLinkedListReverse(list2, node2)) {
196+
System.out.println("Oops2!");
197+
}
198+
199+
DoubleNode node3 = generateRandomDoubleList(len, value);
200+
List<Integer> list3 = getDoubleListOriginOrder(node3);
201+
node3 = reverseDoubleList(node3);
202+
if (!checkDoubleListReverse(list3, node3)) {
203+
System.out.println("Oops3!");
204+
}
205+
206+
DoubleNode node4 = generateRandomDoubleList(len, value);
207+
List<Integer> list4 = getDoubleListOriginOrder(node4);
208+
node4 = reverseDoubleList(node4);
209+
if (!checkDoubleListReverse(list4, node4)) {
210+
System.out.println("Oops4!");
211+
}
212+
213+
}
214+
System.out.println("test finish!");
215+
216+
}
217+
218+
}

src/class04/Code01_LinkedListToQueueAndStack.java renamed to src/class04/Code02_LinkedListToQueueAndStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Queue;
55
import java.util.Stack;
66

7-
public class Code01_LinkedListToQueueAndStack {
7+
public class Code02_LinkedListToQueueAndStack {
88

99
public static class Node<V> {
1010
public V value;

src/class04/Code02_DoubleLinkedListToDeque.java renamed to src/class04/Code03_DoubleLinkedListToDeque.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Deque;
44
import java.util.LinkedList;
55

6-
public class Code02_DoubleLinkedListToDeque {
6+
public class Code03_DoubleLinkedListToDeque {
77

88
public static class Node<V> {
99
public V value;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package class04;
2+
3+
// 测试链接:https://leetcode.com/problems/reverse-nodes-in-k-group/
4+
public class Code04_ReverseNodesInKGroup {
5+
6+
// 不要提交这个类
7+
public static class ListNode {
8+
public int val;
9+
public ListNode next;
10+
}
11+
12+
public static ListNode reverseKGroup(ListNode head, int k) {
13+
ListNode start = head;
14+
ListNode end = getKGroupEnd(start, k);
15+
if (end == null) {
16+
return head;
17+
}
18+
head = end;
19+
reverse(start, end);
20+
ListNode lastEnd = start;
21+
while (lastEnd.next != null) {
22+
start = lastEnd.next;
23+
end = getKGroupEnd(start, k);
24+
if (end == null) {
25+
return head;
26+
}
27+
reverse(start, end);
28+
lastEnd.next = end;
29+
lastEnd = start;
30+
}
31+
return head;
32+
}
33+
34+
public static ListNode getKGroupEnd(ListNode start, int k) {
35+
while (--k != 0 && start != null) {
36+
start = start.next;
37+
}
38+
return start;
39+
}
40+
41+
public static void reverse(ListNode start, ListNode end) {
42+
end = end.next;
43+
ListNode pre = null;
44+
ListNode cur = start;
45+
ListNode next = null;
46+
while (cur != end) {
47+
next = cur.next;
48+
cur.next = pre;
49+
pre = cur;
50+
cur = next;
51+
}
52+
start.next = end;
53+
}
54+
55+
}

src/class04/Code05_AddTwoNumbers.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package class04;
2+
3+
// 测试链接:https://leetcode.com/problems/add-two-numbers/
4+
public class Code05_AddTwoNumbers {
5+
6+
// 不要提交这个类
7+
public static class ListNode {
8+
public int val;
9+
public ListNode next;
10+
11+
public ListNode(int val) {
12+
this.val = val;
13+
}
14+
15+
public ListNode(int val, ListNode next) {
16+
this.val = val;
17+
this.next = next;
18+
}
19+
}
20+
21+
public static ListNode addTwoNumbers(ListNode head1, ListNode head2) {
22+
int len1 = listLength(head1);
23+
int len2 = listLength(head2);
24+
ListNode l = len1 >= len2 ? head1 : head2;
25+
ListNode s = l == head1 ? head2 : head1;
26+
ListNode curL = l;
27+
ListNode curS = s;
28+
ListNode last = curL;
29+
int carry = 0;
30+
int curNum = 0;
31+
while (curS != null) {
32+
curNum = curL.val + curS.val + carry;
33+
curL.val = (curNum % 10);
34+
carry = curNum / 10;
35+
last = curL;
36+
curL = curL.next;
37+
curS = curS.next;
38+
}
39+
while (curL != null) {
40+
curNum = curL.val + carry;
41+
curL.val = (curNum % 10);
42+
carry = curNum / 10;
43+
last = curL;
44+
curL = curL.next;
45+
}
46+
if (carry != 0) {
47+
last.next = new ListNode(1);
48+
}
49+
return l;
50+
}
51+
52+
public static int listLength(ListNode head) {
53+
int len = 0;
54+
while (head != null) {
55+
len++;
56+
head = head.next;
57+
}
58+
return len;
59+
}
60+
61+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package class04;
2+
3+
// 测试链接:https://leetcode.com/problems/merge-two-sorted-lists
4+
public class Code06_MergeTwoSortedLinkedList {
5+
6+
// 不要提交这个类
7+
public static class ListNode {
8+
public int val;
9+
public ListNode next;
10+
}
11+
12+
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
13+
if (l1 == null || l2 == null) {
14+
return l1 == null ? l2 : l1;
15+
}
16+
ListNode head = l1.val <= l2.val ? l1 : l2;
17+
ListNode cur1 = head.next;
18+
ListNode cur2 = head == l1 ? l2 : l1;
19+
ListNode pre = head;
20+
while (cur1 != null && cur2 != null) {
21+
if (cur1.val <= cur2.val) {
22+
pre.next = cur1;
23+
cur1 = cur1.next;
24+
} else {
25+
pre.next = cur2;
26+
cur2 = cur2.next;
27+
}
28+
pre = pre.next;
29+
}
30+
pre.next = cur1 != null ? cur1 : cur2;
31+
return head;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)