-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1721-Swapping_Nodes_in_a_Linked_List.cpp
More file actions
151 lines (137 loc) · 3.3 KB
/
1721-Swapping_Nodes_in_a_Linked_List.cpp
File metadata and controls
151 lines (137 loc) · 3.3 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
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
/*******************************************************************************
* 1721-Swapping_Nodes_in_a_Linked_List.cpp
* Billy.Ljm
* 15 May 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/swapping-nodes-in-a-linked-list/
*
* You are given the head of a linked list, and an integer k.
*
* Return the head of the linked list after swapping the values of the kth node
* from the beginning and the kth node from the end (the list is 1-indexed).
*
* ===========
* My Approach
* ===========
* We will crawl through the linked list, remembering the (k-1)th node and
* the (-k-1)-th node. To do this, we will use two crawlers spaced a appropriate
* number of nodes apart. Then, when one of them reaches the head or tail, the
* other crawler will be at the desired node. We then just have to swap them.
*
* This has a time complexity of O(n) and space complexity of O(1), where n is
* the length of the linked list.
******************************************************************************/
#include <iostream>
#include <vector>
/**
* Definition for singly - linked list
*/
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) {}
};
/**
* Solution
*/
class Solution {
public:
/**
* Swaps the (k)-th and (-k)-th nodes of a linked list
*
* @param head head of the linked list to swap
* @param k index of the nodes to swap
*
* @return head of the swapped linked list
*/
ListNode* swapNodes(ListNode* head, int k) {
ListNode* front;
ListNode* back;
ListNode* ptr;
// find k-th element
ptr = head;
for (int i = 1; i < k; i++) {
ptr = ptr->next;
}
// find (-k)-th element
back = head;
front = ptr;
while (front->next != nullptr) {
back = back->next;
front = front->next;
}
// swap their values
int tmp = ptr->val;
ptr->val = back->val;
back->val = tmp;
return head;
}
};
/**
* Convert from vector to linked list
*
* @param nodes value of linked list nodes in order
*
* @return head of linked list
*/
ListNode* linkedlist(std::vector<int> nodes) {
ListNode* head = nullptr;
for (int i = nodes.size() - 1; i >= 0; i--) {
head = new ListNode(nodes[i], head);
}
return head;
}
/**
* Delete linked list
*
* @param head head of linked list to be deleted
*/
void dellinkedlist(ListNode* head) {
if (head->next == nullptr) {
delete head;
}
else {
dellinkedlist(head->next);
delete head;
}
}
/**
* << operator for ListNodes's linked lisst
*/
std::ostream& operator<<(std::ostream& os, const ListNode* head) {
if (head->next == nullptr) {
return os << head->val;
}
else {
return os << head->val << "," << head->next;
}
}
/**
* Test cases
*/
int main(void) {
Solution sol;
int k;
ListNode* head;
std::vector<int> nodes;
// test case 1
nodes = { 1, 2, 3, 4, 5 };
head = linkedlist(nodes);
k = 2;
std::cout << "swapNodes([" << head << "]," << k << ") = ";
std::cout << "[" << sol.swapNodes(head, k) << "]" << std::endl;
dellinkedlist(head);
// test case 2
nodes = { 7, 9, 6, 6, 7, 8, 3, 0, 9, 5 };
head = linkedlist(nodes);
k = 5;
std::cout << "swapNodes([" << head << "]," << k << ") = ";
std::cout << "[" << sol.swapNodes(head, k) << "]" << std::endl;
dellinkedlist(head);
return 0;
}