forked from kexun/jianzhioffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day08.java
203 lines (159 loc) · 3.8 KB
/
Day08.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.offer;
public class Day08 {
public static void main(String[] args) {
Day08 d = new Day08();
// Node head = d.new Node(0);
//
// Node node1 = d.new Node(1);
// Node node2 = d.new Node(2);
// Node node3 = d.new Node(3);
// Node node4 = d.new Node(4);
// Node node5 = d.new Node(5);
// Node node6 = d.new Node(6);
// Node node7 = d.new Node(7);
// Node node8 = d.new Node(8);
// Node node9 = d.new Node(9);
// Node node10 = d.new Node(10);
// Node node11 = d.new Node(11);
// Node node12 = d.new Node(12);
//
// head.next = node1;
// node1.next = node2;
// node2.next = node3;
// node3.next = node4;
// node4.next = node5;
// node5.next = node6;
// node6.next = node7;
// node7.next = node8;
// node8.next = node9;
// node9.next = node10;
// node10.next = node11;
// node11.next = node12;
// node12.next = null;
// Node n = d.findNodeToTail(head, 12);
// System.out.println(n.data);
// Node h = d.reverseList(head);
// d.printList(h);
Node head1 = d.new Node(0);
Node head2 = d.new Node(0);
Node node1 = d.new Node(1);
Node node2 = d.new Node(2);
Node node3 = d.new Node(3);
Node node4 = d.new Node(4);
Node node5 = d.new Node(5);
Node node6 = d.new Node(6);
Node node7 = d.new Node(7);
Node node8 = d.new Node(8);
Node node9 = d.new Node(9);
Node node10 = d.new Node(10);
Node node11 = d.new Node(11);
Node node12 = d.new Node(12);
Node node13 = d.new Node(2);
Node node14 = d.new Node(3);
Node node15 = d.new Node(4);
Node node16 = d.new Node(5);
Node node17 = d.new Node(6);
head1.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = null;
head2.next = node13;
node13.next = node14;
node14.next = node15;
node15.next = node16;
node16.next = node17;
node17.next = node6;
node6.next = node7;
node7.next = node8;
node8.next = node9;
node9.next = node10;
node10.next = node11;
node11.next = node12;
node12.next = null;
Node list = d.mergeSortedList(head1, head2);
d.printList(list);
}
/**
* 打印单向链表的倒数第n个节点
* @param head
* @param k
* @return
*/
public Node findNodeToTail(Node head, int k) {
if (head == null || head.next == null || k == 0)
return null;
Node node = head;
for (int i = 1; i < k; i++) {
node = node.next;
if (node.next == null)
return null;
}
Node secondNode = head;
while (node.next != null) {
node = node.next;
secondNode = secondNode.next;
}
return secondNode;
}
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
/**
* 反转链表
* @param head
* @return
*/
public Node reverseList(Node head) {
if (head == null || head.next == null)
return null;
Node preNode = null;
Node nextNode = null;
Node node = head.next;
while (node != null) {
nextNode = node.next;
if (preNode != null) {
node.next = preNode;
preNode = node;
} else {
preNode = node;
preNode.next = null;
}
node = nextNode;
}
return preNode;
}
public void printList(Node head) {
Node node = head;
while (node != null) {
System.out.println(node.data);
node = node.next;
}
}
/////////////////////////////////////////////
/**
* 对两个排序好的链表 合并并且排序
* @param first
* @param second
*/
public Node mergeSortedList(Node first, Node second) {
if (first == null)
return second;
if (second == null)
return first;
Node mergedList = null;
if (first.data >= second.data) {
mergedList = second;
mergedList.next = mergeSortedList(first, second.next);
} else {
mergedList = first;
mergedList.next = mergeSortedList(first.next, second);
}
return mergedList;
}
}