forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-2-kthToLast.cpp
161 lines (139 loc) · 3.67 KB
/
2-2-kthToLast.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
/**
* Cracking the coding interview edition 6
* Problem 2.2
* Return kth to last
* Implement an algorithm to find the kth to last element of a singly linked list.
* We can assume we are not provided the length of the list.
*
* Approaches:
* 1. Iterative:
* Use two pointers
* Move first pointer k places.
* Now move second pointer(from start) and first pointer (from k+1) simultaneously.
* When second pointer would have reached end, first pointer would be at kth node.
*
* 2. Recursive:
* Maintain an index to keep track of node.
* Solve the problem for n-1 nodes and add 1 to index.
* Since each parent call is adding 1, when counter reaches k,
* we have reached length-k node from start, which is kth node from last.
*/
#include <iostream>
struct Node {
int data;
Node * next;
Node(int d) : data{ d }, next{ nullptr } { }
};
/**
* Insert to the head of the list
* @param head - Current head of list
* @param data - new node's data
*/
void insert( Node * & head, int data ) {
Node * newNode = new Node(data);
newNode->next = head;
head = newNode;
}
/**
* [deleteList - delete the entire list]
* @param head - head of the list
*/
void deleteList( Node * & head ) {
Node * nextNode;
while(head) {
nextNode = head->next;
delete(head);
head = nextNode;
}
}
/**
* printList - Helper routine to print the list
* @param head - Current head of the list.
*/
void printList( Node * head ) {
while(head) {
std::cout << head->data << "-->";
head = head->next;
}
std::cout << "null" << std::endl;
}
/**
* [kthToLastHelper - helper routine to find nth node for recursive approach
* @param head - head of the list
* @param k - the k value for finding kth element from last of the list.
* @param i - an index maintained to keep track of current node.
* @return - kth node from last.
*/
Node * kthToLastHelper( Node * head, int k , int & i) {
if ( head == nullptr ) {
return nullptr;
}
Node * node = kthToLastHelper(head->next, k, i);
i = i + 1;
//if we have solved problem k times from last.
if ( i == k ) {
return head;
}
return node;
}
/**
* kthToLastRecursive - Recursive approach for finding kth to last element of list.
* @param head - head of node
* @param k - the k value for finding kth element from last of the list.
* @return - kth node from last.
*/
Node * kthToLastRecursive( Node * head, int k ) {
int i = 0;
return kthToLastHelper(head, k, i);
}
/**
* kthToLastIterative - Iterative approach for finding kth to last element of list.
* @param head - head of node
* @param k - the k value for finding kth element from last of the list.
* @return - kth node from last.
*/
Node * kthToLastIterative( Node * head, int k ) {
if ( head == nullptr ) {
return head;
}
Node * ptr1 = head;
Node * ptr2 = head;
int i = 0;
while( i < k && ptr1 ) {
ptr1 = ptr1->next;
++i;
}
//out of bounds
if ( i < k ) {
return nullptr;
}
while( ptr1 != nullptr ) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return ptr2;
}
int main() {
Node * head = nullptr;
for ( int i = 7; i > 0; i-- ) {
insert(head, i);
}
std::cout << "List: ";
printList(head);
std::cout << "4th node from last (Recursive) : ";
Node *node4 = kthToLastRecursive(head, 4);
if ( node4 != nullptr ) {
std::cout << node4->data << std::endl;
} else {
std::cout << "NULL NODE\n";
}
std::cout << "4th node from last (Iterative) : ";
node4 = kthToLastIterative(head, 4);
if ( node4 != nullptr ) {
std::cout << node4->data << std::endl;
} else {
std::cout << "NULL NODE\n";
}
deleteList(head);
return 0;
}