-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathDay16.java
161 lines (135 loc) · 3.06 KB
/
Day16.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
package com.offer;
/**
* 复杂链表的复制
* @author kexun
*
*/
public class Day16 {
public static void main(String[] args) {
Day16 d = new Day16();
Node header = d.new Node(-1);
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);
header.next = node1;
node1.next = node2;
node1.sibling = node6;
node2.next = node3;
node2.sibling = node3;
node3.next = node4;
node3.sibling = node8;
node4.next = node5;
node4.sibling = node1;
node5.next = node6;
node5.sibling = node9;
node6.next = node7;
node6.sibling = node4;
node7.next = node8;
node7.sibling = node2;
node8.next = node9;
node8.sibling = node8;
node9.next = node10;
node10.next = node11;
d.cloneList(header);
d.cloneSibling(header);
Node clone = d.devide(header);
d.printList(clone);
System.out.println("------------");
d.printList(header);
}
/**
* 复杂链表的节点, next 指向下一个节点
* sibling随意指向一个节点,也可以指向null
* @author kexun
*
*/
class Node {
public int data;
public Node next;
public Node sibling;
public Node(int data) {
this.data = data;
}
}
/**
* 给定一个复制链表,复制每个节点,复制方式如下 A->A1->B->B1->C->C1
* @param header
*/
public void cloneList(Node header) {
if (header == null)
return;
Node node = header.next;
while (node != null) {
Node cloneNode = new Node(node.data);
cloneNode.next = node.next;
node.next = cloneNode;
node = cloneNode.next;
}
}
/**
* 复制每个节点的sibling指针
* @param header
*/
public void cloneSibling(Node header) {
if (header == null)
return;
Node node = header.next;
while (node != null) {
Node cloneNode = node.next;
if (node.sibling != null) {
cloneNode.sibling = node.sibling.next;
}
node = cloneNode.next;
}
}
/**
* 把之前合在一起的链表,拆分成两个链表
* @param header
* @return
*/
public Node devide(Node header) {
if (header == null)
return null;
Node node = header.next;
Node cloneHeader = new Node(-1);
Node cloneNode = null;
if (node != null) {
cloneHeader.next = cloneNode = node.next;
node.next = cloneNode.next;
node = cloneNode.next;
}
while (node != null) {
cloneNode.next = node.next;
cloneNode = cloneNode.next;
node.next = cloneNode.next;
node = node.next;
}
return cloneHeader;
}
/**
* 打印复制链表
* @param header
*/
public void printList(Node header) {
if (header == null)
return;
Node node = header.next;
while (node != null) {
Node temp = node.sibling;
if (temp != null) {
System.out.println("data="+node.data+" si="+temp.data);
} else {
System.out.println("data="+node.data+" si=null");
}
node = node.next;
}
}
}