LINKED LISTS IN C PROGRAMMING
Linked List Fundamentals
What is a Linked List?
A linked list is a linear data structure where elements are stored in nodes, and each node points to the next node in the sequence. Unlike arrays, linked lists don't require contiguous memory allocation.
- Dynamic Size - Can grow or shrink during program execution
- Efficient Insertions/Deletions - No need to shift elements
- Memory Efficiency - Allocates memory as needed
// Basic Node Structure in C
struct Node {
int data;
struct Node* next;
};
Types of Linked Lists
Singly Linked List
Each node points only to the next node in sequence.
struct Node {
int data;
struct Node* next;
};
Doubly Linked List
Each node has pointers to both next and previous nodes.
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
Circular Linked List
Last node points back to the first node (can be singly or doubly linked).
// Circular variation of
// either singly or doubly
// linked list
Linked List Operations
| # | Operation | Description | Complexity | Code Snippet |
|---|---|---|---|---|
| 1 | Insert at Head | Add new node at the beginning of the list | O(1) |
|
| 2 | Insert at Tail | Add new node at the end of the list | O(n) |
|
| 3 | Delete by Value | Remove first occurrence of a specific value | O(n) |
|
| 4 | Search Element | Check if a value exists in the list | O(n) |
|
| 5 | Traversal | Visit each node of the list | O(n) |
|
| 6 | Reverse List | Reverse the order of nodes in the list | O(n) |
|
Advanced Linked List Techniques
Cycle Detection (Floyd's Algorithm)
Detect cycles in a linked list using fast and slow pointers.
bool hasCycle(struct Node *head) {
struct Node *slow = head, *fast = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if(slow == fast) return true;
}
return false;
}
Find Middle Element
Find the middle node using fast and slow pointers.
struct Node* findMiddle(struct Node* head) {
struct Node *slow = head, *fast = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
Merge Two Sorted Lists
Merge two sorted linked lists into one sorted list.
struct Node* mergeLists(struct Node* l1, struct Node* l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
if(l1->data < l2->data) {
l1->next = mergeLists(l1->next, l2);
return l1;
} else {
l2->next = mergeLists(l1, l2->next);
return l2;
}
}
Remove Nth Node from End
Remove the nth node from the end of the list.
struct Node* removeNthFromEnd(struct Node* head, int n) {
struct Node *fast = head, *slow = head;
for(int i = 0; i < n; i++) fast = fast->next;
if(fast == NULL) return head->next;
while(fast->next != NULL) {
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return head;
}
Palindrome Check
Check if a linked list is a palindrome.
bool isPalindrome(struct Node* head) {
struct Node *slow = head, *fast = head, *prev = NULL, *temp;
// Find middle and reverse first half
while(fast && fast->next) {
fast = fast->next->next;
temp = slow->next;
slow->next = prev;
prev = slow;
slow = temp;
}
// Handle odd length
if(fast) slow = slow->next;
// Compare two halves
while(slow) {
if(prev->data != slow->data) return false;
prev = prev->next;
slow = slow->next;
}
return true;
}
Intersection Point
Find the intersection node of two linked lists.
struct Node *getIntersectionNode(struct Node *headA, struct Node *headB) {
struct Node *a = headA, *b = headB;
while(a != b) {
a = a ? a->next : headB;
b = b ? b->next : headA;
}
return a;
}
Linked List Problem-Solving Patterns
| # | Pattern | Description | Example Problems |
|---|---|---|---|
| 1 | Two Pointer Technique | Use fast and slow pointers to solve problems with optimal time and space complexity |
|
| 2 | Dummy Node Technique | Create a dummy node to simplify edge cases in list manipulation |
|
| 3 | Recursion | Solve problems by breaking them down into smaller subproblems |
|
| 4 | Pointer Manipulation | Carefully manipulate node pointers to achieve desired structure |
|
| 5 | Hybrid Data Structures | Combine linked lists with other data structures for efficient solutions |
|