Skip to content

Commit

Permalink
Merge pull request #58 from Samyak137/main
Browse files Browse the repository at this point in the history
linked list important questions
  • Loading branch information
gautamankoji authored Oct 28, 2024
2 parents d7cf74d + 0bd0fbe commit 7d86832
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 0 deletions.
71 changes: 71 additions & 0 deletions interview_questions/linked-list/merge_list.cpp
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;
}
51 changes: 51 additions & 0 deletions interview_questions/linked-list/middle_of_linked_list.cpp
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 interview_questions/linked-list/remove_nth_node_from_end.cpp
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;
}
65 changes: 65 additions & 0 deletions interview_questions/linked-list/reverse_list.cpp
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 interview_questions/linked-list/reverse_node_in_kGroup.cpp
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;
}

0 comments on commit 7d86832

Please sign in to comment.