-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-8.cpp
More file actions
116 lines (101 loc) · 2.94 KB
/
Copy pathtask-8.cpp
File metadata and controls
116 lines (101 loc) · 2.94 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
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
#include <iostream>
#include <unordered_map>
#include <stack>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) { data = val; next = nullptr; }
};
class SinglyLinkedList {
private:
Node* head;
public:
SinglyLinkedList() { head = nullptr; }
Node* getHead() { return head; }
void setHead(Node* h) { head = h; }
void insertAtTail(int val) {
Node* newNode = new Node(val);
if (!head) { head = newNode; return; }
Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
void display() {
Node* temp = head;
while (temp) { cout << temp->data << " -> "; temp = temp->next; }
cout << "NULL\n";
}
bool isPalindrome() {
if (!head || !head->next) return true;
stack<int> s;
Node* temp = head;
while (temp) { s.push(temp->data); temp = temp->next; }
temp = head;
while (temp) { if (temp->data != s.top()) return false; s.pop(); temp = temp->next; }
return true;
}
void rotate(int k) {
if (!head || k == 0) return;
Node* temp = head;
int len = 1;
while (temp->next) { temp = temp->next; len++; }
k = k % len;
if (k == 0) return;
temp->next = head;
Node* newTail = head;
for (int i = 0; i < len - k - 1; i++) newTail = newTail->next;
head = newTail->next;
newTail->next = nullptr;
}
};
class RandomNode {
public:
int val;
RandomNode* next;
RandomNode* random;
RandomNode(int x) { val = x; next = nullptr; random = nullptr; }
};
RandomNode* copyRandomList(RandomNode* head) {
if (!head) return nullptr;
unordered_map<RandomNode*, RandomNode*> m;
RandomNode* temp = head;
while (temp) { m[temp] = new RandomNode(temp->val); temp = temp->next; }
temp = head;
while (temp) {
m[temp]->next = temp->next ? m[temp->next] : nullptr;
m[temp]->random = temp->random ? m[temp->random] : nullptr;
temp = temp->next;
}
return m[head];
}
int main() {
SinglyLinkedList list;
list.insertAtTail(1);
list.insertAtTail(2);
list.insertAtTail(3);
list.insertAtTail(2);
list.insertAtTail(1);
cout << "Original List: ";
list.display();
cout << "Is Palindrome? " << (list.isPalindrome() ? "YES" : "NO") << endl;
list.rotate(2);
cout << "After rotating by 2: ";
list.display();
RandomNode* r1 = new RandomNode(1);
RandomNode* r2 = new RandomNode(2);
RandomNode* r3 = new RandomNode(3);
r1->next = r2; r2->next = r3;
r1->random = r3; r2->random = r1; r3->random = r2;
RandomNode* copied = copyRandomList(r1);
RandomNode* temp = copied;
cout << "Copied Random List: ";
while (temp) {
cout << temp->val << "(R:";
cout << (temp->random ? temp->random->val : -1) << ") -> ";
temp = temp->next;
}
cout << "NULL\n";
return 0;
}