Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d4dc36a

Browse files
add q138
1 parent 594c279 commit d4dc36a

File tree

6 files changed

+121
-16
lines changed

6 files changed

+121
-16
lines changed

‎.idea/workspace.xml‎

Lines changed: 26 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
* [q2_两数相加](/src/链表操作/q2_两数相加)
1212
* [q206_反转链表](/src/链表操作/q206_反转链表)
13+
* [q138_复制带随机指针的链表](/src/链表操作/q138_复制带随机指针的链表)
1314

1415
### 双指针遍历/滑动窗口
1516

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package 链表操作.q138_复制带随机指针的链表.f1;
2+
3+
class Node {
4+
int val;
5+
Node next;
6+
Node random;
7+
8+
public Node(int val) {
9+
this.val = val;
10+
this.next = null;
11+
this.random = null;
12+
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package 链表操作.q138_复制带随机指针的链表.f1;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* 用Map存储遍历过的节点,时间o(n),额外空间o(n)
7+
*/
8+
public class Solution {
9+
10+
HashMap<Node, Node> visitedHash = new HashMap<Node, Node>();
11+
12+
public Node copyRandomList(Node head) {
13+
14+
if (head == null) {
15+
return null;
16+
}
17+
if (this.visitedHash.containsKey(head)) {
18+
return this.visitedHash.get(head);
19+
}
20+
21+
Node node = new Node(head.val);
22+
23+
this.visitedHash.put(head, node);
24+
node.next = this.copyRandomList(head.next);
25+
node.random = this.copyRandomList(head.random);
26+
27+
return node;
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package 链表操作.q138_复制带随机指针的链表.f2;
2+
3+
class Node {
4+
int val;
5+
Node next;
6+
Node random;
7+
8+
public Node(int val) {
9+
this.val = val;
10+
this.next = null;
11+
this.random = null;
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package 链表操作.q138_复制带随机指针的链表.f2;
2+
3+
/**
4+
* 在每一个链表的节点后都新连一个节点之后操作 时间o(n) 额外空间o(1)
5+
*/
6+
public class Solution {
7+
public Node copyRandomList(Node head) {
8+
9+
if (head == null) {
10+
return null;
11+
}
12+
13+
Node ptr = head;
14+
while (ptr != null) {
15+
Node newNode = new Node(ptr.val);
16+
newNode.next = ptr.next;
17+
ptr.next = newNode;
18+
ptr = newNode.next;
19+
}
20+
21+
ptr = head;
22+
23+
while (ptr != null) {
24+
ptr.next.random = (ptr.random != null) ? ptr.random.next : null;
25+
ptr = ptr.next.next;
26+
}
27+
28+
Node ptrOldList = head;
29+
Node ptrNewList = head.next;
30+
Node headOld = head.next;
31+
while (ptrOldList != null) {
32+
ptrOldList.next = ptrOldList.next.next;
33+
ptrNewList.next = (ptrNewList.next != null) ? ptrNewList.next.next : null;
34+
ptrOldList = ptrOldList.next;
35+
ptrNewList = ptrNewList.next;
36+
}
37+
return headOld;
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /