Skip to content

Commit 0f3b4bc

Browse files
ADD: Linked list code
1 parent 808e421 commit 0f3b4bc

31 files changed

+82
-8
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Node {
6+
public:
7+
int data;
8+
Node* next;
9+
Node(int data) {
10+
this->data = data;
11+
this->next = NULL;
12+
}
13+
};
14+
15+
/**
16+
* Both recursive and iterative approach can be used
17+
*
18+
*/
19+
Node* insertAtHead(Node* head, int data) {
20+
if(head==NULL) {
21+
return new Node(data);
22+
}
23+
head->next = insertAtHead(head->next, data);
24+
return head;
25+
}
26+
27+
int main() {
28+
int arr[] = {1, 2, 3, 4, 5};
29+
Node* head = NULL;
30+
for(int i: arr) {
31+
head = insertAtHead(head, i);
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Node {
6+
public:
7+
int data;
8+
Node* next;
9+
Node(int data) {
10+
this->data = data;
11+
this->next = NULL;
12+
}
13+
};
14+
15+
Node* insertAtHead(Node* head, int data) {
16+
if(head==NULL) {
17+
return new Node(data);
18+
}
19+
head->next = insertAtHead(head->next, data);
20+
return head;
21+
}
22+
23+
void printLinkedList(Node* head) {
24+
while(head!=NULL) {
25+
cout<<head->data<<" ";
26+
head = head->next;
27+
}
28+
}
29+
30+
int main() {
31+
int arr[] = {1, 2, 3, 4, 5};
32+
Node* head = NULL;
33+
for(int i: arr) {
34+
head = insertAtHead(head, i);
35+
}
36+
printLinkedList(head);
37+
}

data-structures/bst/operations/find/closest_in_bst.cpp renamed to data-structures/tree/bst/operations/find/closest_in_bst.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@ class Node {
1616
};
1717

1818
int closest = INT_MAX;
19+
int nearestValue;
1920
/**
20-
* COMPLEXITY: O(Height) - in case of recursion we will traverse till the last
21-
* O(1) - in case of iterative approach
21+
* TIME-COMPLEXITY: O(N)
22+
* SPACE-COMPLEXITY: O(Height) - recursion | O(1) - iterative
2223
* LOGIC: Pick the required branch on each step
2324
*
2425
*/
2526
int closestInBST(Node* node, int key){
2627
if(node==NULL) {
27-
return closest;
28+
return nearestValue;
2829
}
2930
int currClosest = abs(node->data - key);
31+
if(currClosest == 0) {
32+
return node->data;
33+
}
34+
3035
if(closest > currClosest) {
31-
closest = node->data;
36+
nearestValue = node->data;
37+
closest = currClosest;
3238
}
3339
if (node->data > key) {
3440
return closestInBST(node->left, key);
@@ -45,8 +51,8 @@ int main() {
4551
root->left->right = new Node(6);
4652
root->left->right->left = new Node(4);
4753
root->left->right->right = new Node(7);
48-
root->right->right = new Node(12);
49-
root->right->right->right = new Node(23);
54+
root->right->right = new Node(15);
55+
root->right->right->right = new Node(25);
5056
root->right->right->left = new Node(13);
5157
cout<<closestInBST(root, 16);
5258
}

tempCodeRunnerFile.cpp

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)