-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from Samyak137/main
linked list important questions
- Loading branch information
Showing
5 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Title: Merge Two Sorted Linked Lists | ||
|
||
// Problem: Given two sorted linked lists, merge them into a single sorted linked list. | ||
// Approach: Use a two-pointer approach to compare nodes and merge them into a new list. | ||
|
||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode* next; | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
}; | ||
|
||
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { | ||
if (!l1) return l2; | ||
if (!l2) return l1; | ||
|
||
if (l1->val < l2->val) { | ||
l1->next = mergeTwoLists(l1->next, l2); | ||
return l1; | ||
} else { | ||
l2->next = mergeTwoLists(l1, l2->next); | ||
return l2; | ||
} | ||
} | ||
|
||
void printList(ListNode* head) { | ||
while (head) { | ||
cout << head->val << " "; | ||
head = head->next; | ||
} | ||
cout << endl; | ||
} | ||
|
||
ListNode* createList(int n) { | ||
int value; | ||
ListNode* head = nullptr; | ||
ListNode* tail = nullptr; | ||
cout << "Enter values in sorted order: "; | ||
for (int i = 0; i < n; ++i) { | ||
cin >> value; | ||
ListNode* newNode = new ListNode(value); | ||
if (!head) { | ||
head = newNode; | ||
tail = newNode; | ||
} else { | ||
tail->next = newNode; | ||
tail = newNode; | ||
} | ||
} | ||
return head; | ||
} | ||
|
||
int main() { | ||
int n1, n2; | ||
cout << "Enter number of nodes in first sorted list: "; | ||
cin >> n1; | ||
ListNode* l1 = createList(n1); | ||
|
||
cout << "Enter number of nodes in second sorted list: "; | ||
cin >> n2; | ||
ListNode* l2 = createList(n2); | ||
|
||
ListNode* mergedHead = mergeTwoLists(l1, l2); | ||
cout << "Merged list: "; | ||
printList(mergedHead); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Title: Find the Middle of a Linked List | ||
|
||
// Problem: Find the middle node of a linked list. If there are two middle nodes, return the second one. | ||
// Approach: Use two pointers, one moving one step at a time (slow) and the other moving two steps at a time (fast). | ||
|
||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode* next; | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
}; | ||
|
||
ListNode* findMiddle(ListNode* head) { | ||
ListNode* slow = head; | ||
ListNode* fast = head; | ||
while (fast && fast->next) { | ||
slow = slow->next; | ||
fast = fast->next->next; | ||
} | ||
return slow; | ||
} | ||
|
||
int main() { | ||
int n, value; | ||
cout << "Enter the number of nodes in the list: "; | ||
cin >> n; | ||
|
||
ListNode* head = nullptr; | ||
ListNode* tail = nullptr; | ||
|
||
cout << "Enter values for each node: "; | ||
for (int i = 0; i < n; ++i) { | ||
cin >> value; | ||
ListNode* newNode = new ListNode(value); | ||
if (!head) { | ||
head = newNode; | ||
tail = newNode; | ||
} else { | ||
tail->next = newNode; | ||
tail = newNode; | ||
} | ||
} | ||
|
||
ListNode* middleNode = findMiddle(head); | ||
cout << "The middle node's value is: " << middleNode->val << endl; | ||
|
||
return 0; | ||
} |
74 changes: 74 additions & 0 deletions
74
interview_questions/linked-list/remove_nth_node_from_end.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Title: Remove N-th Node from End of List | ||
// Problem: Given the head of a linked list, remove the N-th node from the end of the list. | ||
|
||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode* next; | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
}; | ||
|
||
ListNode* removeNthFromEnd(ListNode* head, int n) { | ||
ListNode* dummy = new ListNode(0); | ||
dummy->next = head; | ||
ListNode* first = dummy; | ||
ListNode* second = dummy; | ||
|
||
// Move first n+1 steps ahead | ||
for (int i = 0; i <= n; ++i) { | ||
first = first->next; | ||
} | ||
|
||
// Move both pointers until first reaches the end | ||
while (first) { | ||
first = first->next; | ||
second = second->next; | ||
} | ||
|
||
// Skip the desired node | ||
second->next = second->next->next; | ||
|
||
return dummy->next; | ||
} | ||
|
||
void printList(ListNode* head) { | ||
while (head) { | ||
cout << head->val << " "; | ||
head = head->next; | ||
} | ||
cout << endl; | ||
} | ||
|
||
int main() { | ||
int n, nodes, value; | ||
cout << "Enter the number of nodes: "; | ||
cin >> nodes; | ||
|
||
ListNode* head = nullptr; | ||
ListNode* tail = nullptr; | ||
|
||
cout << "Enter values for each node: "; | ||
for (int i = 0; i < nodes; ++i) { | ||
cin >> value; | ||
ListNode* newNode = new ListNode(value); | ||
if (!head) { | ||
head = newNode; | ||
tail = newNode; | ||
} else { | ||
tail->next = newNode; | ||
tail = newNode; | ||
} | ||
} | ||
|
||
cout << "Enter the value of n (from end): "; | ||
cin >> n; | ||
|
||
head = removeNthFromEnd(head, n); | ||
cout << "List after removing " << n << "-th node from end: "; | ||
printList(head); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Title: Reverse List | ||
// Problem: Given list have to print in reverse order | ||
|
||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode* next; | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
}; | ||
|
||
ListNode* reverseList(ListNode* head) { | ||
ListNode* prev = nullptr; | ||
ListNode* curr = head; | ||
while (curr) { | ||
ListNode* nextTemp = curr->next; | ||
curr->next = prev; | ||
prev = curr; | ||
curr = nextTemp; | ||
} | ||
return prev; | ||
} | ||
|
||
void printList(ListNode* head) { | ||
while (head) { | ||
cout << head->val << " "; | ||
head = head->next; | ||
} | ||
cout << endl; | ||
} | ||
|
||
int main() { | ||
int n, value; | ||
cout << "Enter the number of nodes: "; | ||
cin >> n; | ||
|
||
ListNode* head = nullptr; | ||
ListNode* tail = nullptr; | ||
|
||
cout << "Enter the values: "; | ||
for (int i = 0; i < n; ++i) { | ||
cin >> value; | ||
ListNode* newNode = new ListNode(value); | ||
if (!head) { | ||
head = newNode; | ||
tail = newNode; | ||
} else { | ||
tail->next = newNode; | ||
tail = newNode; | ||
} | ||
} | ||
|
||
cout << "Original list: "; | ||
printList(head); | ||
|
||
ListNode* reversedHead = reverseList(head); | ||
|
||
|
||
cout << "Reversed list: "; | ||
printList(reversedHead); | ||
|
||
return 0; | ||
} |
74 changes: 74 additions & 0 deletions
74
interview_questions/linked-list/reverse_node_in_kGroup.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Title: Reverse Nodes in k-Group | ||
|
||
// Problem: Given a linked list, reverse nodes in groups of k. | ||
// Approach: Reverse every k nodes iteratively, then move to the next k nodes. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode* next; | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
}; | ||
|
||
ListNode* reverseKGroup(ListNode* head, int k) { | ||
ListNode* curr = head; | ||
int count = 0; | ||
while (curr && count < k) { | ||
curr = curr->next; | ||
count++; | ||
} | ||
if (count < k) return head; | ||
|
||
curr = head; | ||
ListNode* prev = nullptr; | ||
ListNode* next = nullptr; | ||
for (int i = 0; i < k; ++i) { | ||
next = curr->next; | ||
curr->next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
head->next = reverseKGroup(curr, k); | ||
return prev; | ||
} | ||
|
||
void printList(ListNode* head) { | ||
while (head) { | ||
cout << head->val << " "; | ||
head = head->next; | ||
} | ||
cout << endl; | ||
} | ||
|
||
int main() { | ||
int n, k, value; | ||
cout << "Enter the number of nodes: "; | ||
cin >> n; | ||
|
||
ListNode* head = nullptr; | ||
ListNode* tail = nullptr; | ||
|
||
cout << "Enter values for each node: "; | ||
for (int i = 0; i < n; ++i) { | ||
cin >> value; | ||
ListNode* newNode = new ListNode(value); | ||
if (!head) { | ||
head = newNode; | ||
tail = newNode; | ||
} else { | ||
tail->next = newNode; | ||
tail = newNode; | ||
} | ||
} | ||
|
||
cout << "Enter value of k: "; | ||
cin >> k; | ||
|
||
ListNode* newHead = reverseKGroup(head, k); | ||
cout << "List after reversing every k group: "; | ||
printList(newHead); | ||
|
||
return 0; | ||
} |