-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_kth_node_from_end.cpp
96 lines (83 loc) · 1.89 KB
/
print_kth_node_from_end.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
#include <iostream>
using namespace std;
class Node{
public:
int val;
Node* next;
Node(int data = 0){
this->val = data;
this->next = NULL;
}
~Node(){
cout<<"Deleted: "<<this->val<<endl;
this->next = NULL;
delete next;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;
}
int length(Node* &head){
Node* temp = head;
int len = 0;
while(temp){
len++;
temp = temp->next;
}
return len;
}
// Iterative Apporach
Node* printKthNode(Node* &head, int k){
int len = length(head);
if(head == NULL || k > len)
return NULL;
int kth = len - k - 1;
Node* temp = head;
while(kth-- && temp)
temp = temp->next;
return temp;
}
// Recursive Apporach
void printKthNode_01(Node* temp, int &k, int &ans){
if(temp == NULL)
return;
printKthNode_01(temp->next,k,ans);
if(k == 0)
ans = temp->val;
k--;
}
int main()
{
Node* head = new Node(121);
Node* sec = new Node(2);
Node* third = new Node(33);
Node* forth = new Node(16);
Node* fifth = new Node(55);
Node* sixth = new Node(69);
Node* seventh = new Node(26);
Node* eighth = new Node(90);
Node* ninth = new Node(223);
Node* tenth = new Node(689);
head->next = sec;
sec->next = third;
third->next = forth;
forth->next = fifth;
fifth->next = sixth;
sixth->next = seventh;
seventh->next = eighth;
eighth->next = ninth;
ninth->next = tenth;
tenth->next = NULL;
// Node* temp = printKthNode(head,0);
// cout<<temp->val<<endl;
int ans;
int k = 6;
printKthNode_01(head,k,ans);
cout<<ans<<endl;
return 0;
}