Skip to content

Commit c8d4405

Browse files
committed
leetcode
1 parent b775d6d commit c8d4405

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
3+
-* 138. Copy List with Random Pointer *-
4+
5+
6+
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
7+
8+
Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
9+
10+
For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
11+
12+
Return the head of the copied linked list.
13+
14+
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
15+
16+
val: an integer representing Node.val
17+
random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
18+
Your code will only be given the head of the original linked list.
19+
20+
21+
22+
Example 1:
23+
24+
25+
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
26+
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
27+
Example 2:
28+
29+
30+
Input: head = [[1,1],[2,1]]
31+
Output: [[1,1],[2,1]]
32+
Example 3:
33+
34+
35+
36+
Input: head = [[3,null],[3,0],[3,null]]
37+
Output: [[3,null],[3,0],[3,null]]
38+
39+
40+
Constraints:
41+
42+
0 <= n <= 1000
43+
-104 <= Node.val <= 104
44+
Node.random is null or is pointing to some node in the linked list.
45+
46+
47+
*/
48+
49+
class Node {
50+
int val;
51+
Node? next;
52+
Node? random;
53+
54+
Node(this.val, {this.next, this.random});
55+
}
56+
57+
void insertAtTail(Node? head, Node? tail, int data) {
58+
final Node? temp = Node(data);
59+
if (head == null) {
60+
head = temp;
61+
tail = temp;
62+
return;
63+
}
64+
tail!.next = temp;
65+
tail = temp;
66+
}
67+
68+
Node? copyRandomList(Node? head) {
69+
if (head == null) {
70+
return null;
71+
}
72+
73+
// Step 1: Create a clone linked list
74+
Node? temp = head;
75+
Node? cloneHead;
76+
Node? cloneTail;
77+
while (temp != null) {
78+
insertAtTail(cloneHead, cloneTail, temp.val);
79+
temp = temp.next;
80+
}
81+
82+
// Step 2: Connect links between clone and original list
83+
Node? cloneNode = cloneHead;
84+
Node? originalNode = head;
85+
while (originalNode != null && cloneNode != null) {
86+
final originalNext = originalNode.next;
87+
originalNode.next = cloneNode;
88+
originalNode = originalNext;
89+
90+
final cloneNext = cloneNode.next;
91+
cloneNode.next = originalNode;
92+
cloneNode = cloneNext;
93+
}
94+
95+
// Step 3: Connect random pointers
96+
temp = head;
97+
while (temp != null) {
98+
if (temp.random != null) {
99+
temp.next!.random = temp.random!.next;
100+
}
101+
temp = temp.next!.next;
102+
}
103+
104+
// Revert Step 2
105+
originalNode = head;
106+
cloneNode = cloneHead;
107+
while (originalNode != null && cloneNode != null) {
108+
originalNode.next = cloneNode.next;
109+
originalNode = originalNode.next!;
110+
cloneNode.next = originalNode.next;
111+
cloneNode = cloneNode.next!;
112+
}
113+
114+
// Return the cloned head
115+
return cloneHead;
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
type Node struct {
4+
Val int
5+
Next *Node
6+
Random *Node
7+
}
8+
9+
func insertAtTail(head **Node, tail **Node, data int) {
10+
temp := &Node{Val: data}
11+
if *head == nil {
12+
*head = temp
13+
*tail = temp
14+
return
15+
}
16+
(*tail).Next = temp
17+
*tail = temp
18+
}
19+
20+
func copyRandomList(head *Node) *Node {
21+
if head == nil {
22+
return nil
23+
}
24+
25+
// Step 1: Create a clone linked list
26+
temp := head
27+
var cloneHead *Node
28+
var cloneTail *Node
29+
for temp != nil {
30+
insertAtTail(&cloneHead, &cloneTail, temp.Val)
31+
temp = temp.Next
32+
}
33+
34+
// Step 2: Connect links between clone and original list
35+
cloneNode := cloneHead
36+
originalNode := head
37+
for originalNode != nil && cloneNode != nil {
38+
originalNext := originalNode.Next
39+
originalNode.Next = cloneNode
40+
originalNode = originalNext
41+
42+
cloneNext := cloneNode.Next
43+
cloneNode.Next = originalNode
44+
cloneNode = cloneNext
45+
}
46+
47+
// Step 3: Connect random pointers
48+
temp = head
49+
for temp != nil {
50+
if temp.Random != nil {
51+
temp.Next.Random = temp.Random.Next
52+
}
53+
temp = temp.Next.Next
54+
}
55+
56+
// Revert Step 2
57+
originalNode = head
58+
cloneNode = cloneHead
59+
for originalNode != nil && cloneNode != nil {
60+
originalNode.Next = cloneNode.Next
61+
originalNode = originalNode.Next
62+
if originalNode != nil {
63+
cloneNode.Next = originalNode.Next
64+
}
65+
cloneNode = cloneNode.Next
66+
}
67+
68+
// Return the cloned head
69+
return cloneHead
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [DART](https://leetcode.com/problems/copy-list-with-random-pointer/solutions/4003948/o-n-simple-fast-and-easy-with-explanation/)

0 commit comments

Comments
 (0)