-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemove-Kth-Node-from-Last.cpp
54 lines (48 loc) · 1.2 KB
/
Remove-Kth-Node-from-Last.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node()
{
this->data = 0;
next = NULL;
}
Node(int data)
{
this->data = data;
this->next = NULL;
}
Node(int data, Node* next)
{
this->data = data;
this->next = next;
}
};
Node* removeKthNode(Node* head, int K)
{
// Create a dummy node and point it to the head
Node* newHead = new Node(0);
// Point the next of dummy node to the head
newHead->next = head;
// Create two pointers left and right and point them to dummy node and head respectively
Node* left = newHead, *right = head;
int counter = 0;
// Iterate over the list until right becomes NULL
while(right) {
right = right->next;
// When counter becomes greater than or equal to K, start moving left pointer
if(counter >= K) {
left = left->next;
}
// Increment the counter
counter++;
}
// Remove the Kth node from the last, which is the node pointed by
// next pointer of left pointer
left->next = left->next->next;
// Return the head of the list
return newHead->next;
}