File tree 5 files changed +335
-0
lines changed
interview_questions/linked-list
5 files changed +335
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Title: Merge Two Sorted Linked Lists
2
+
3
+ // Problem: Given two sorted linked lists, merge them into a single sorted linked list.
4
+ // Approach: Use a two-pointer approach to compare nodes and merge them into a new list.
5
+
6
+
7
+ #include < iostream>
8
+ using namespace std ;
9
+
10
+ struct ListNode {
11
+ int val;
12
+ ListNode* next;
13
+ ListNode (int x) : val(x), next(nullptr ) {}
14
+ };
15
+
16
+ ListNode* mergeTwoLists (ListNode* l1, ListNode* l2) {
17
+ if (!l1) return l2;
18
+ if (!l2) return l1;
19
+
20
+ if (l1->val < l2->val ) {
21
+ l1->next = mergeTwoLists (l1->next , l2);
22
+ return l1;
23
+ } else {
24
+ l2->next = mergeTwoLists (l1, l2->next );
25
+ return l2;
26
+ }
27
+ }
28
+
29
+ void printList (ListNode* head) {
30
+ while (head) {
31
+ cout << head->val << " " ;
32
+ head = head->next ;
33
+ }
34
+ cout << endl;
35
+ }
36
+
37
+ ListNode* createList (int n) {
38
+ int value;
39
+ ListNode* head = nullptr ;
40
+ ListNode* tail = nullptr ;
41
+ cout << " Enter values in sorted order: " ;
42
+ for (int i = 0 ; i < n; ++i) {
43
+ cin >> value;
44
+ ListNode* newNode = new ListNode (value);
45
+ if (!head) {
46
+ head = newNode;
47
+ tail = newNode;
48
+ } else {
49
+ tail->next = newNode;
50
+ tail = newNode;
51
+ }
52
+ }
53
+ return head;
54
+ }
55
+
56
+ int main () {
57
+ int n1, n2;
58
+ cout << " Enter number of nodes in first sorted list: " ;
59
+ cin >> n1;
60
+ ListNode* l1 = createList (n1);
61
+
62
+ cout << " Enter number of nodes in second sorted list: " ;
63
+ cin >> n2;
64
+ ListNode* l2 = createList (n2);
65
+
66
+ ListNode* mergedHead = mergeTwoLists (l1, l2);
67
+ cout << " Merged list: " ;
68
+ printList (mergedHead);
69
+
70
+ return 0 ;
71
+ }
Original file line number Diff line number Diff line change
1
+ // Title: Find the Middle of a Linked List
2
+
3
+ // Problem: Find the middle node of a linked list. If there are two middle nodes, return the second one.
4
+ // Approach: Use two pointers, one moving one step at a time (slow) and the other moving two steps at a time (fast).
5
+
6
+
7
+ #include < iostream>
8
+ using namespace std ;
9
+
10
+ struct ListNode {
11
+ int val;
12
+ ListNode* next;
13
+ ListNode (int x) : val(x), next(nullptr ) {}
14
+ };
15
+
16
+ ListNode* findMiddle (ListNode* head) {
17
+ ListNode* slow = head;
18
+ ListNode* fast = head;
19
+ while (fast && fast->next ) {
20
+ slow = slow->next ;
21
+ fast = fast->next ->next ;
22
+ }
23
+ return slow;
24
+ }
25
+
26
+ int main () {
27
+ int n, value;
28
+ cout << " Enter the number of nodes in the list: " ;
29
+ cin >> n;
30
+
31
+ ListNode* head = nullptr ;
32
+ ListNode* tail = nullptr ;
33
+
34
+ cout << " Enter values for each node: " ;
35
+ for (int i = 0 ; i < n; ++i) {
36
+ cin >> value;
37
+ ListNode* newNode = new ListNode (value);
38
+ if (!head) {
39
+ head = newNode;
40
+ tail = newNode;
41
+ } else {
42
+ tail->next = newNode;
43
+ tail = newNode;
44
+ }
45
+ }
46
+
47
+ ListNode* middleNode = findMiddle (head);
48
+ cout << " The middle node's value is: " << middleNode->val << endl;
49
+
50
+ return 0 ;
51
+ }
Original file line number Diff line number Diff line change
1
+ // Title: Remove N-th Node from End of List
2
+ // Problem: Given the head of a linked list, remove the N-th node from the end of the list.
3
+
4
+
5
+ #include < iostream>
6
+ using namespace std ;
7
+
8
+ struct ListNode {
9
+ int val;
10
+ ListNode* next;
11
+ ListNode (int x) : val(x), next(nullptr ) {}
12
+ };
13
+
14
+ ListNode* removeNthFromEnd (ListNode* head, int n) {
15
+ ListNode* dummy = new ListNode (0 );
16
+ dummy->next = head;
17
+ ListNode* first = dummy;
18
+ ListNode* second = dummy;
19
+
20
+ // Move first n+1 steps ahead
21
+ for (int i = 0 ; i <= n; ++i) {
22
+ first = first->next ;
23
+ }
24
+
25
+ // Move both pointers until first reaches the end
26
+ while (first) {
27
+ first = first->next ;
28
+ second = second->next ;
29
+ }
30
+
31
+ // Skip the desired node
32
+ second->next = second->next ->next ;
33
+
34
+ return dummy->next ;
35
+ }
36
+
37
+ void printList (ListNode* head) {
38
+ while (head) {
39
+ cout << head->val << " " ;
40
+ head = head->next ;
41
+ }
42
+ cout << endl;
43
+ }
44
+
45
+ int main () {
46
+ int n, nodes, value;
47
+ cout << " Enter the number of nodes: " ;
48
+ cin >> nodes;
49
+
50
+ ListNode* head = nullptr ;
51
+ ListNode* tail = nullptr ;
52
+
53
+ cout << " Enter values for each node: " ;
54
+ for (int i = 0 ; i < nodes; ++i) {
55
+ cin >> value;
56
+ ListNode* newNode = new ListNode (value);
57
+ if (!head) {
58
+ head = newNode;
59
+ tail = newNode;
60
+ } else {
61
+ tail->next = newNode;
62
+ tail = newNode;
63
+ }
64
+ }
65
+
66
+ cout << " Enter the value of n (from end): " ;
67
+ cin >> n;
68
+
69
+ head = removeNthFromEnd (head, n);
70
+ cout << " List after removing " << n << " -th node from end: " ;
71
+ printList (head);
72
+
73
+ return 0 ;
74
+ }
Original file line number Diff line number Diff line change
1
+ // Title: Reverse List
2
+ // Problem: Given list have to print in reverse order
3
+
4
+
5
+ #include < iostream>
6
+ using namespace std ;
7
+
8
+ struct ListNode {
9
+ int val;
10
+ ListNode* next;
11
+ ListNode (int x) : val(x), next(nullptr ) {}
12
+ };
13
+
14
+ ListNode* reverseList (ListNode* head) {
15
+ ListNode* prev = nullptr ;
16
+ ListNode* curr = head;
17
+ while (curr) {
18
+ ListNode* nextTemp = curr->next ;
19
+ curr->next = prev;
20
+ prev = curr;
21
+ curr = nextTemp;
22
+ }
23
+ return prev;
24
+ }
25
+
26
+ void printList (ListNode* head) {
27
+ while (head) {
28
+ cout << head->val << " " ;
29
+ head = head->next ;
30
+ }
31
+ cout << endl;
32
+ }
33
+
34
+ int main () {
35
+ int n, value;
36
+ cout << " Enter the number of nodes: " ;
37
+ cin >> n;
38
+
39
+ ListNode* head = nullptr ;
40
+ ListNode* tail = nullptr ;
41
+
42
+ cout << " Enter the values: " ;
43
+ for (int i = 0 ; i < n; ++i) {
44
+ cin >> value;
45
+ ListNode* newNode = new ListNode (value);
46
+ if (!head) {
47
+ head = newNode;
48
+ tail = newNode;
49
+ } else {
50
+ tail->next = newNode;
51
+ tail = newNode;
52
+ }
53
+ }
54
+
55
+ cout << " Original list: " ;
56
+ printList (head);
57
+
58
+ ListNode* reversedHead = reverseList (head);
59
+
60
+
61
+ cout << " Reversed list: " ;
62
+ printList (reversedHead);
63
+
64
+ return 0 ;
65
+ }
Original file line number Diff line number Diff line change
1
+ // Title: Reverse Nodes in k-Group
2
+
3
+ // Problem: Given a linked list, reverse nodes in groups of k.
4
+ // Approach: Reverse every k nodes iteratively, then move to the next k nodes.
5
+
6
+ #include < iostream>
7
+ using namespace std ;
8
+
9
+ struct ListNode {
10
+ int val;
11
+ ListNode* next;
12
+ ListNode (int x) : val(x), next(nullptr ) {}
13
+ };
14
+
15
+ ListNode* reverseKGroup (ListNode* head, int k) {
16
+ ListNode* curr = head;
17
+ int count = 0 ;
18
+ while (curr && count < k) {
19
+ curr = curr->next ;
20
+ count++;
21
+ }
22
+ if (count < k) return head;
23
+
24
+ curr = head;
25
+ ListNode* prev = nullptr ;
26
+ ListNode* next = nullptr ;
27
+ for (int i = 0 ; i < k; ++i) {
28
+ next = curr->next ;
29
+ curr->next = prev;
30
+ prev = curr;
31
+ curr = next;
32
+ }
33
+ head->next = reverseKGroup (curr, k);
34
+ return prev;
35
+ }
36
+
37
+ void printList (ListNode* head) {
38
+ while (head) {
39
+ cout << head->val << " " ;
40
+ head = head->next ;
41
+ }
42
+ cout << endl;
43
+ }
44
+
45
+ int main () {
46
+ int n, k, value;
47
+ cout << " Enter the number of nodes: " ;
48
+ cin >> n;
49
+
50
+ ListNode* head = nullptr ;
51
+ ListNode* tail = nullptr ;
52
+
53
+ cout << " Enter values for each node: " ;
54
+ for (int i = 0 ; i < n; ++i) {
55
+ cin >> value;
56
+ ListNode* newNode = new ListNode (value);
57
+ if (!head) {
58
+ head = newNode;
59
+ tail = newNode;
60
+ } else {
61
+ tail->next = newNode;
62
+ tail = newNode;
63
+ }
64
+ }
65
+
66
+ cout << " Enter value of k: " ;
67
+ cin >> k;
68
+
69
+ ListNode* newHead = reverseKGroup (head, k);
70
+ cout << " List after reversing every k group: " ;
71
+ printList (newHead);
72
+
73
+ return 0 ;
74
+ }
You can’t perform that action at this time.
0 commit comments