Linked List in C1
Linked List in C1
A linked list is a fundamental data structure where elements (nodes) are linked together
using pointers. Unlike arrays, linked lists are dynamic, meaning memory is allocated as
needed.
This guide will take you from basic to advanced concepts with step-by-step explanations,
examples, and C code.
🛠 Structure of a Node in C
struct Node {
int data;
};
Example Representation:
struct Node {
int data;
};
Example Representation:
Singly Circular Linked List → Last node’s next points to the first node.
Doubly Circular Linked List → Last node’s next points to the first, and first’s prev
points to the last.
Steps:
1. Create a structure Node with data and next.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
printList(head);
return 0;
}
Output:
Steps:
3. Return count.
Code:
Length: 3
Steps:
3. Update head.
Steps:
Steps:
Found at position 2
Steps:
Steps:
Approach: Use the Floyd’s Cycle Detection Algorithm (Tortoise and Hare).
Use two pointers: slow moves one step, fast moves two steps.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int detectCycle(struct Node* head) {
struct Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return 1; // Cycle detected
}
return 0; // No cycle
}
Problem: Given a linked list, check if it reads the same forwards and backwards.
Approach:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* reverse(struct Node* head) {
struct Node *prev = NULL, *curr = head, *next = NULL;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
int isPalindrome(struct Node* head) {
if (!head || !head->next) return 1;
struct Node *slow = head, *fast = head, *prev = NULL;
while (fast && fast->next) {
prev = slow;
slow = slow->next;
fast = fast->next->next;
}
prev->next = NULL; // Split the list
struct Node* secondHalf = reverse(slow);
struct Node* firstHalf = head;
while (firstHalf && secondHalf) {
if (firstHalf->data != secondHalf->data) return 0;
firstHalf = firstHalf->next;
secondHalf = secondHalf->next;
}
return 1;
}
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(2);
head->next->next->next = createNode(1);
if (isPalindrome(head))
printf("The linked list is a palindrome\n");
else
printf("The linked list is NOT a palindrome\n");
return 0;
}
Output:
The linked list is a palindrome
Problem: Merge K sorted linked lists into one sorted linked list.
Approach:
Use a Min Heap (Priority Queue) to always extract the smallest node.
Insert the next node of the extracted node back into the heap.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
// Merge two sorted lists
return l1;
} else {
return l2;
if (k == 0) return NULL;
while (k > 1) {
k = (k + 1) / 2;
return lists[0];
newNode->data = data;
newNode->next = NULL;
return newNode;
}
while (head) {
head = head->next;
printf("NULL\n");
int main() {
int k = 3;
lists[0] = createNode(1);
lists[0]->next = createNode(4);
lists[0]->next->next = createNode(7);
lists[1] = createNode(2);
lists[1]->next = createNode(5);
lists[1]->next->next = createNode(8);
lists[2] = createNode(3);
lists[2]->next = createNode(6);
lists[2]->next->next = createNode(9);
printList(mergedHead);
return 0;
}
Output:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL
Problem: Given a linked list, reverse every K nodes in a group. If the number of remaining
nodes is less than K, leave them as they are.
Approach:
3. Recursively call the function for the remaining part of the list.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
💡 Approach:
✅ Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
return l1;
} else {
return l2;
fast = fast->next->next;
return slow;
middle->next = NULL;
// Create node
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Print function
while (head) {
head = head->next;
printf("NULL\n");
int main() {
head->next = createNode(2);
head->next->next = createNode(1);
head->next->next->next = createNode(3);
printList(head);
head = sortList(head);
printList(head);
return 0;
📝 Output:
📌 Problem: Given a linked list where each node has a random pointer that points to another
node in the list, create a deep copy.
💡 Approach:
✅ Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
while (curr) {
copy->data = curr->data;
copy->next = curr->next;
curr->next = copy;
curr = copy->next;
}
curr = head;
while (curr) {
if (curr->random)
curr->next->random = curr->random->next;
curr = curr->next->next;
curr = head;
while (curr) {
curr->next = curr->next->next;
if (copy->next)
copy->next = copy->next->next;
curr = curr->next;
copy = copy->next;
return newHead;
Let's go step by step and cover each of these advanced linked list problems in C with
detailed explanations, approaches, and working code.
📌 Problem: Given a linked list, reverse every K nodes in a group. If the number of remaining
nodes is less than K, leave them as they are.
💡 Approach:
3. Recursively call the function for the remaining part of the list.
✅ Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
int count = 0;
// Count K nodes
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
return prev;
newNode->data = data;
newNode->next = NULL;
return newNode;
head = head->next;
printf("NULL\n");
int main() {
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
int k = 2;
printList(head);
printList(head);
return 0;
📝 Output:
💡 Approach:
✅ Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
return l1;
} else {
return l2;
}
// Function to find middle node
slow = slow->next;
fast = fast->next->next;
return slow;
middle->next = NULL;
// Create node
newNode->next = NULL;
return newNode;
// Print function
while (head) {
head = head->next;
printf("NULL\n");
int main() {
head->next = createNode(2);
head->next->next = createNode(1);
head->next->next->next = createNode(3);
printList(head);
head = sortList(head);
printList(head);
return 0;
}
📝 Output:
📌 Problem: Given a linked list where each node has a random pointer that points to another
node in the list, create a deep copy.
💡 Approach:
✅ Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
while (curr) {
struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
copy->data = curr->data;
copy->next = curr->next;
curr->next = copy;
curr = copy->next;
curr = head;
while (curr) {
if (curr->random)
curr->next->random = curr->random->next;
curr = curr->next->next;
curr = head;
while (curr) {
curr->next = curr->next->next;
if (copy->next)
copy->next = copy->next->next;
curr = curr->next;
copy = copy->next;
return newHead;
}
🚀 More Problems Coming Next