Skip to content

Commit f5a3eed

Browse files
committedApr 19, 2019
复杂链表的复制
1 parent 65cc3e2 commit f5a3eed

File tree

6 files changed

+152
-2
lines changed

6 files changed

+152
-2
lines changed
 

‎LeetCode/Doc/单词切分2.md

-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ ArrayList<String> wordBreak(String s, Set<String> dict){
5656

5757
注意,第一步切割bcdefg的时候递归调用了wordBreak,在切割bcdefg的时候也会切割cdefg、defg、efg、fg、g,然后下面几步也是类似的操作,所以说下面几步实际上做了一部分重复的工作,为了避免这种重复的工作,我们可以使用Map,将s与其切分的结果存下来,这样下次再遇到s的时候直接从map中取之前分好的结果就行了,不用做重复操作。
5858

59-
想上面那样的思路写出来的代码和牛客网评测系统给出的字符串的顺序不一样,观察了一下刚好和上面的顺序是反的,只要把从前到后遍历改成从后到前遍历即可。
60-
6159

6260
## 代码
6361

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# copy-list-with-random-pointer(拷贝具有随机指针的链表)
2+
3+
<center>知识点:链表</center>
4+
5+
6+
## 题目描述
7+
8+
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
9+
10+
Return a deep copy of the list.
11+
12+
13+
给出链表,每个节点包含一个额外的随机指针,该指针可以指向列表中的任何节点或为空。
14+
15+
返回列表的深拷贝。
16+
17+
18+
## 解题思路
19+
20+
![image-20190226161741675](https://ws4.sinaimg.cn/large/006tKfTcgy1g0jwcpj055j31ho0ea0ux.jpg)
21+
22+
上图为原始链表,黑色箭头代表next指针,绿色箭头代表random指针。
23+
24+
25+
26+
第一步,复制每一个节点并将其插入到原来的链表中:
27+
28+
![image-20190226161821940](https://ws4.sinaimg.cn/large/006tKfTcgy1g0jwde8vhqj31ho0latbo.jpg)
29+
30+
红色的节点即为复制的节点
31+
32+
第二步,根据原来节点的random指针更新复制的节点的random:
33+
34+
```python
35+
currentNode = pHead
36+
while currentNode is not None:
37+
node = currentNode.next
38+
if currentNode.random is not None:
39+
node.random = currentNode.random.next
40+
currentNode = node.next
41+
```
42+
43+
第三步:分离出复制的节点们构成复制的链表:
44+
45+
```python
46+
cloneP = pHead.next
47+
48+
while currentNode.next is not None:
49+
temp = currentNode.next
50+
currentNode.next = temp.next
51+
currentNode = temp
52+
```
53+
54+
![image-20190226162013065](https://ws2.sinaimg.cn/large/006tKfTcgy1g0jwfboa5zj31ho0emq55.jpg)
55+
56+
57+
58+
## 代码
59+
60+
[这里](../src/thirteen/Solution.java)
Binary file not shown.
Binary file not shown.

‎LeetCode/src/thirteen/Solution.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package thirteen;
2+
3+
/**
4+
* @author dmrfcoder
5+
* @date 2019-04-19
6+
*/
7+
8+
/**
9+
* Definition for singly-linked list with a random pointer.
10+
*/
11+
class RandomListNode {
12+
int label;
13+
RandomListNode next, random;
14+
15+
RandomListNode(int x) {
16+
this.label = x;
17+
}
18+
};
19+
20+
public class Solution {
21+
public RandomListNode copyRandomList(RandomListNode head) {
22+
23+
if (head == null) {
24+
return null;
25+
}
26+
RandomListNode curNode = head;
27+
RandomListNode curNodeNext;
28+
while (curNode != null) {
29+
RandomListNode copyOfCurNode = new RandomListNode(curNode.label);
30+
curNodeNext = curNode.next;
31+
curNode.next = copyOfCurNode;
32+
copyOfCurNode.next = curNodeNext;
33+
curNode = curNode.next.next;
34+
}
35+
36+
curNode = head;
37+
38+
while (curNode != null) {
39+
curNode.next.random = curNode.random;
40+
curNode = curNode.next.next;
41+
}
42+
43+
RandomListNode copyListHead = head.next;
44+
RandomListNode curNewNode = head.next;
45+
RandomListNode tempNode;
46+
47+
curNode = head;
48+
49+
while (curNode != null) {
50+
tempNode = curNewNode.next;
51+
curNode.next = tempNode;
52+
if (tempNode != null) {
53+
curNewNode.next = tempNode.next;
54+
} else {
55+
curNewNode.next = null;
56+
}
57+
58+
curNode = tempNode;
59+
if (curNode!=null){
60+
curNewNode = curNode.next;
61+
}
62+
63+
64+
}
65+
66+
return copyListHead;
67+
}
68+
69+
public static void main(String[] args) {
70+
RandomListNode head = new RandomListNode(-1);
71+
RandomListNode node2 = new RandomListNode(1);
72+
RandomListNode node3 = new RandomListNode(3);
73+
RandomListNode node4 = new RandomListNode(4);
74+
RandomListNode node5 = new RandomListNode(5);
75+
head.next = node2;
76+
//node2.next = node3;
77+
// node3.next = node4;
78+
// node4.next = node5;
79+
//
80+
// head.random = node5;
81+
// node2.random = head;
82+
// node3.random = null;
83+
// node4.random = node3;
84+
// node5.random = node2;
85+
86+
Solution s = new Solution();
87+
RandomListNode randomListNode = s.copyRandomList(head);
88+
89+
int a = 0;
90+
}
91+
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,5 @@
154154
- [linked-list-cycle(判断链表中是否存在环)](./LeetCode/Doc/判断链表中是否存在环.md)
155155
- [word-break(单词切分)](./LeetCode/Doc/单词切分.md)
156156
- [word-break-ii(单词切分2)](./LeetCode/Doc/单词切分2.md)
157+
- [copy-list-with-random-pointer(拷贝具有随机指针的链表)](./LeetCode/Doc/拷贝具有随机指针的链表.md)
157158

0 commit comments

Comments
 (0)