-
Notifications
You must be signed in to change notification settings - Fork 13
/
LC_107_SwappingNodesInALinkedList.cpp
169 lines (145 loc) · 3.38 KB
/
LC_107_SwappingNodesInALinkedList.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
void insertAtHead(ListNode *&head, ListNode *&tail, int data)
{
// check for Empty LL
if (head == NULL)
{
ListNode *newNode = new ListNode(data);
head = newNode;
tail = newNode;
}
else
{
// step1:
ListNode *newNode = new ListNode(data);
// step2:
newNode->next = head;
// step3:
head = newNode;
}
}
void print(ListNode *head)
{
ListNode *temp = head;
while (temp != NULL)
{
cout << temp->val << " ";
temp = temp->next;
}
}
int findLength(ListNode *&head)
{
ListNode *temp = head;
int len = 0;
while (temp != NULL)
{
len++;
temp = temp->next;
}
return len;
}
// swap nodes without swapping data i.e. only swapping pointers
// tc: O(n), sc: O(1)
ListNode *swapNodes(ListNode *head, int k)
{
// case 1
if (!head || !head->next)
return head;
int n = findLength(head);
int lpos = k, rpos = n - k + 1;
if (lpos > rpos)
swap(lpos, rpos);
// case 2
if (lpos == rpos)
return head;
// case 3
if (n == 2)
{
ListNode *second = head->next;
head->next = NULL;
second->next = head;
return second; // new head
}
// case 4
if (lpos == 1)
{
ListNode *lp = 0, *ln = head, *rp = head;
for (int i = 0; i < rpos - 2; i++)
rp = rp->next;
;
ListNode *rn = rp->next;
ListNode *rSave = rn->next;
// swappping
rn->next = ln->next;
rp->next = ln;
ln->next = rSave;
return rn; // new head
}
// case 5
int intermediateNodes = rpos - lpos - 1;
if (intermediateNodes == 0)
{
// ln and rn are adjacent
ListNode *lp = head;
for (int i = 0; i < lpos - 2; i++)
lp = lp->next;
ListNode *ln = lp->next;
ListNode *rp = ln;
ListNode *rn = rp->next;
ListNode *rSave = rn->next;
// swapping
lp->next = rp->next;
rn->next = rp;
rp->next = rSave;
return head; // new head
}
else
{
// case 6
// if intermediate nodes are present i.e. intermediateNodes >=1
ListNode *lp = head;
for (int i = 0; i < lpos - 2; i++)
lp = lp->next;
ListNode *ln = lp->next;
ListNode *rp = head;
for (int i = 0; i < rpos - 2; i++)
rp = rp->next;
ListNode *rn = rp->next;
ListNode *rSave = rn->next;
// swapping
lp->next = rn;
rn->next = ln->next;
rp->next = ln;
ln->next = rSave;
return head; // new head
}
}
int main()
{
ListNode *head = NULL;
ListNode *tail = NULL;
insertAtHead(head, tail, 7);
insertAtHead(head, tail, 6);
insertAtHead(head, tail, 5);
insertAtHead(head, tail, 4);
insertAtHead(head, tail, 3);
insertAtHead(head, tail, 2);
insertAtHead(head, tail, 1);
cout << "Original LL: ";
print(head);
cout << endl;
cout << "Swapped LL: ";
ListNode *newHead = swapNodes(head, 2);
print(newHead);
cout << endl;
return 0;
}