Solution and explanation
Expected output
Q31 Intersection: 8
Q32 Sum digits: 7 -> 0 -> 8 -> NULL
Q33 Sorted: 1 -> 2 -> 3 -> 4 -> NULL
Q34 Rotated: 4 -> 5 -> 1 -> 2 -> 3 -> NULL
Q35 Groups of 3: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7 -> NULL
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node* createNode(int value) {
Node* n = malloc(sizeof(Node));
if (!n) exit(EXIT_FAILURE);
n->data = value;
n->next = NULL;
return n;
}
Node* insertEnd(Node* head, int value) {
Node* n = createNode(value);
if (!head) return n;
Node* cur = head;
while (cur->next) cur = cur->next;
cur->next = n;
return head;
}
void display(Node* head) {
while (head) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
// Q31: Find the shared intersection node by identity, not value.
Node* getIntersection(Node* a, Node* b) {
Node *p = a, *q = b;
while (p != q) {
p = p ? p->next : b;
q = q ? q->next : a;
}
return p;
}
// Q32: Add reverse-order digit lists.
Node* addNumbers(Node* a, Node* b) {
Node dummy = {0, NULL};
Node* tail = &dummy;
int carry = 0;
while (a || b || carry) {
int sum = carry;
if (a) { sum += a->data; a = a->next; }
if (b) { sum += b->data; b = b->next; }
carry = sum / 10;
tail->next = createNode(sum % 10);
tail = tail->next;
}
return dummy.next;
}
// Q33: Merge sort
Node* mergeLists(Node* a, Node* b) {
Node dummy = {0, NULL};
Node* tail = &dummy;
while (a && b) {
if (a->data <= b->data) {
tail->next = a;
a = a->next;
} else {
tail->next = b;
b = b->next;
}
tail = tail->next;
}
tail->next = a ? a : b;
return dummy.next;
}
Node* mergeSort(Node* head) {
if (!head || !head->next) return head;
Node *slow = head, *fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
Node* right = slow->next;
slow->next = NULL;
return mergeLists(mergeSort(head), mergeSort(right));
}
// Q34: Rotate right by k positions
Node* rotateRight(Node* head, int k) {
if (!head || !head->next || k <= 0) return head;
int length = 1;
Node* tail = head;
while (tail->next) {
tail = tail->next;
length++;
}
k %= length;
if (k == 0) return head;
tail->next = head;
int steps = length - k;
Node* newTail = tail;
while (steps--) newTail = newTail->next;
Node* newHead = newTail->next;
newTail->next = NULL;
return newHead;
}
// Q35: Reverse nodes in groups of k.
// An incomplete final group remains unchanged.
Node* reverseKGroup(Node* head, int k) {
if (!head || k <= 1) return head;
Node* check = head;
for (int i = 0; i < k; i++) {
if (!check) return head;
check = check->next;
}
Node *prev = NULL, *cur = head;
for (int i = 0; i < k; i++) {
Node* next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
head->next = reverseKGroup(cur, k);
return prev;
}
void freeList(Node* head) {
while (head) {
Node* next = head->next;
free(head);
head = next;
}
}
int main(void) {
// Q31
Node* shared = NULL;
shared = insertEnd(shared, 8);
shared = insertEnd(shared, 10);
Node* a = NULL;
a = insertEnd(a, 3);
a = insertEnd(a, 6);
Node* atail = a;
while (atail->next) atail = atail->next;
atail->next = shared;
Node* b = NULL;
b = insertEnd(b, 4);
Node* btail = b;
while (btail->next) btail = btail->next;
btail->next = shared;
Node* intersection = getIntersection(a, b);
printf("Q31 Intersection: %d\n",
intersection ? intersection->data : -1);
// The two lists share nodes. Break their links before freeing.
atail->next = NULL;
btail->next = NULL;
freeList(a);
freeList(b);
freeList(shared);
// Q32
Node *num1 = NULL, *num2 = NULL;
num1 = insertEnd(num1, 2);
num1 = insertEnd(num1, 4);
num1 = insertEnd(num1, 3);
num2 = insertEnd(num2, 5);
num2 = insertEnd(num2, 6);
num2 = insertEnd(num2, 4);
Node* sum = addNumbers(num1, num2);
printf("Q32 Sum digits: ");
display(sum);
// Q33 (index loop: range-based for is C++ only)
Node* unsorted = NULL;
int values[] = {4, 2, 1, 3};
for (int i = 0; i < 4; i++)
unsorted = insertEnd(unsorted, values[i]);
unsorted = mergeSort(unsorted);
printf("Q33 Sorted: ");
display(unsorted);
// Q34
Node* rotated = NULL;
for (int x = 1; x <= 5; x++)
rotated = insertEnd(rotated, x);
rotated = rotateRight(rotated, 2);
printf("Q34 Rotated: ");
display(rotated);
// Q35
Node* grouped = NULL;
for (int x = 1; x <= 7; x++)
grouped = insertEnd(grouped, x);
grouped = reverseKGroup(grouped, 3);
printf("Q35 Groups of 3: ");
display(grouped);
freeList(num1);
freeList(num2);
freeList(sum);
freeList(unsorted);
freeList(rotated);
freeList(grouped);
return 0;
}
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
Node* insertEnd(Node* head, int value) {
Node* n = new Node(value);
if (!head) return n;
Node* cur = head;
while (cur->next) cur = cur->next;
cur->next = n;
return head;
}
void display(Node* head) {
while (head) {
cout << head->data << " -> ";
head = head->next;
}
cout << "NULL\n";
}
// Q31
Node* getIntersection(Node* a, Node* b) {
Node *p = a, *q = b;
while (p != q) {
p = p ? p->next : b;
q = q ? q->next : a;
}
return p;
}
// Q32
Node* addNumbers(Node* a, Node* b) {
Node dummy(0);
Node* tail = &dummy;
int carry = 0;
while (a || b || carry) {
int sum = carry;
if (a) { sum += a->data; a = a->next; }
if (b) { sum += b->data; b = b->next; }
carry = sum / 10;
tail->next = new Node(sum % 10);
tail = tail->next;
}
return dummy.next;
}
// Q33
Node* mergeLists(Node* a, Node* b) {
Node dummy(0);
Node* tail = &dummy;
while (a && b) {
if (a->data <= b->data) {
tail->next = a;
a = a->next;
} else {
tail->next = b;
b = b->next;
}
tail = tail->next;
}
tail->next = a ? a : b;
return dummy.next;
}
Node* mergeSort(Node* head) {
if (!head || !head->next) return head;
Node *slow = head, *fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
Node* right = slow->next;
slow->next = nullptr;
return mergeLists(mergeSort(head), mergeSort(right));
}
// Q34
Node* rotateRight(Node* head, int k) {
if (!head || !head->next || k <= 0) return head;
int length = 1;
Node* tail = head;
while (tail->next) {
tail = tail->next;
length++;
}
k %= length;
if (k == 0) return head;
tail->next = head;
int steps = length - k;
Node* newTail = tail;
while (steps--) newTail = newTail->next;
Node* newHead = newTail->next;
newTail->next = nullptr;
return newHead;
}
// Q35: Leave incomplete final group unchanged.
Node* reverseKGroup(Node* head, int k) {
if (!head || k <= 1) return head;
Node* check = head;
for (int i = 0; i < k; i++) {
if (!check) return head;
check = check->next;
}
Node *prev = nullptr, *cur = head;
for (int i = 0; i < k; i++) {
Node* next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
head->next = reverseKGroup(cur, k);
return prev;
}
void freeList(Node* head) {
while (head) {
Node* next = head->next;
delete head;
head = next;
}
}
int main() {
// Q31: Both lists share the same actual nodes.
Node* shared = nullptr;
shared = insertEnd(shared, 8);
shared = insertEnd(shared, 10);
Node* a = nullptr;
a = insertEnd(a, 3);
a = insertEnd(a, 6);
Node* atail = a;
while (atail->next) atail = atail->next;
atail->next = shared;
Node* b = insertEnd(nullptr, 4);
Node* btail = b;
btail->next = shared;
Node* intersection = getIntersection(a, b);
cout << "Q31 Intersection: "
<< (intersection ? intersection->data : -1) << '\n';
atail->next = nullptr;
btail->next = nullptr;
freeList(a);
freeList(b);
freeList(shared);
// Q32
Node *num1 = nullptr, *num2 = nullptr;
for (int x : {2, 4, 3}) num1 = insertEnd(num1, x);
for (int x : {5, 6, 4}) num2 = insertEnd(num2, x);
Node* sum = addNumbers(num1, num2);
cout << "Q32 Sum digits: ";
display(sum);
// Q33
Node* unsorted = nullptr;
for (int x : {4, 2, 1, 3}) unsorted = insertEnd(unsorted, x);
unsorted = mergeSort(unsorted);
cout << "Q33 Sorted: ";
display(unsorted);
// Q34
Node* rotated = nullptr;
for (int x = 1; x <= 5; x++) rotated = insertEnd(rotated, x);
rotated = rotateRight(rotated, 2);
cout << "Q34 Rotated: ";
display(rotated);
// Q35
Node* grouped = nullptr;
for (int x = 1; x <= 7; x++) grouped = insertEnd(grouped, x);
grouped = reverseKGroup(grouped, 3);
cout << "Q35 Groups of 3: ";
display(grouped);
freeList(num1);
freeList(num2);
freeList(sum);
freeList(unsorted);
freeList(rotated);
freeList(grouped);
return 0;
}
Java
public class LinkedList31To35 {
static class Node {
int data;
Node next;
Node(int value) { data = value; }
}
static Node insertEnd(Node head, int value) {
Node n = new Node(value);
if (head == null) return n;
Node cur = head;
while (cur.next != null) cur = cur.next;
cur.next = n;
return head;
}
static void display(Node head) {
while (head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.println("NULL");
}
// Q31
static Node getIntersection(Node a, Node b) {
Node p = a, q = b;
while (p != q) {
p = (p == null) ? b : p.next;
q = (q == null) ? a : q.next;
}
return p;
}
// Q32
static Node addNumbers(Node a, Node b) {
Node dummy = new Node(0);
Node tail = dummy;
int carry = 0;
while (a != null || b != null || carry != 0) {
int sum = carry;
if (a != null) {
sum += a.data;
a = a.next;
}
if (b != null) {
sum += b.data;
b = b.next;
}
carry = sum / 10;
tail.next = new Node(sum % 10);
tail = tail.next;
}
return dummy.next;
}
// Q33
static Node mergeLists(Node a, Node b) {
Node dummy = new Node(0);
Node tail = dummy;
while (a != null && b != null) {
if (a.data <= b.data) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = (a != null) ? a : b;
return dummy.next;
}
static Node mergeSort(Node head) {
if (head == null || head.next == null) return head;
Node slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Node right = slow.next;
slow.next = null;
return mergeLists(mergeSort(head), mergeSort(right));
}
// Q34: Rotate right by k
static Node rotateRight(Node head, int k) {
if (head == null || head.next == null || k <= 0) return head;
int length = 1;
Node tail = head;
while (tail.next != null) {
tail = tail.next;
length++;
}
k %= length;
if (k == 0) return head;
tail.next = head;
int steps = length - k;
Node newTail = tail;
while (steps-- > 0) newTail = newTail.next;
Node newHead = newTail.next;
newTail.next = null;
return newHead;
}
// Q35: Incomplete final group stays unchanged.
static Node reverseKGroup(Node head, int k) {
if (head == null || k <= 1) return head;
Node check = head;
for (int i = 0; i < k; i++) {
if (check == null) return head;
check = check.next;
}
Node prev = null, cur = head;
for (int i = 0; i < k; i++) {
Node next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
head.next = reverseKGroup(cur, k);
return prev;
}
public static void main(String[] args) {
// Q31
Node shared = null;
shared = insertEnd(shared, 8);
shared = insertEnd(shared, 10);
Node a = null;
a = insertEnd(a, 3);
a = insertEnd(a, 6);
Node atail = a;
while (atail.next != null) atail = atail.next;
atail.next = shared;
Node b = new Node(4);
b.next = shared;
Node intersection = getIntersection(a, b);
System.out.println("Q31 Intersection: " +
(intersection == null ? -1 : intersection.data));
atail.next = null;
b.next = null; // Disconnect B's own node from the shared nodes.
// Q32
Node num1 = null, num2 = null;
for (int x : new int[]{2, 4, 3}) num1 = insertEnd(num1, x);
for (int x : new int[]{5, 6, 4}) num2 = insertEnd(num2, x);
Node sum = addNumbers(num1, num2);
System.out.print("Q32 Sum digits: ");
display(sum);
// Q33
Node unsorted = null;
for (int x : new int[]{4, 2, 1, 3})
unsorted = insertEnd(unsorted, x);
unsorted = mergeSort(unsorted);
System.out.print("Q33 Sorted: ");
display(unsorted);
// Q34
Node rotated = null;
for (int x = 1; x <= 5; x++) rotated = insertEnd(rotated, x);
rotated = rotateRight(rotated, 2);
System.out.print("Q34 Rotated: ");
display(rotated);
// Q35
Node grouped = null;
for (int x = 1; x <= 7; x++) grouped = insertEnd(grouped, x);
grouped = reverseKGroup(grouped, 3);
System.out.print("Q35 Groups of 3: ");
display(grouped);
}
}
Note: the C version builds the Q33 list with an index loop; a range-based for (int x : …) is C++ only and does not compile as C.