-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsasheet155.cpp
More file actions
55 lines (44 loc) · 1.19 KB
/
dsasheet155.cpp
File metadata and controls
55 lines (44 loc) · 1.19 KB
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
class Solution
{
public:
Node *copyList(Node *start)
{
//Write your code here
Node* curr = start, *temp;
// insert additional node after
// every node of original list
while (curr)
{
temp = curr->next;
// Inserting node
curr->next = new Node(curr->data);
curr->next->next = temp;
curr = temp;
}
curr = start;
// adjust the arb pointers of the
// newly added nodes
while (curr)
{
if(curr->next)
curr->next->arb = curr->arb ?
curr->arb->next : curr->arb;
// move to the next newly added node by
// skipping an original node
curr = curr->next?curr->next->next:curr->next;
}
Node* original = start, *copy = start->next;
// save the start of copied linked list
temp = copy;
// now separate the original list and copied list
while (original && copy)
{
original->next =
original->next? original->next->next : original->next;
copy->next = copy->next?copy->next->next:copy->next;
original = original->next;
copy = copy->next;
}
return temp;
}
};