Linked List

- Fundamentals -

What is a Linked List?

A LinkedList is a linear data structure consisting of a sequence of elements, called nodes, where each node contains two parts: data (the value or payload) and a reference (or link) to the next node in the sequence.

Unlike arrays, linked lists do not store elements in contiguous memory locations; instead, nodes are connected through pointers, allowing dynamic memory allocation and flexible insertion and deletion operations.

    Key Components

  • Node: The basic unit of a linked list, containing:

    • Data: The value stored (e.g., integer, string, or object).
    • Reference/Pointer: A link to the next node (or null for the last node).
  • Head: The first node in the list, used as the entry point.
  • Tail: The last node, which points to null.
OperationLinked List Array
Access by index✖ O(n) ✔ O(1)
Insert/delete at start✔ O(1) ✖ O(n)
Insert/delete at end✔ O(1)✖ O(n) worst
Insert/delete in middle✔ O(1)✖ O(n)
Dynamic resizing✔ Native✖ May require copy/resize
Memory locality (cache)✖ Poor✔ Excellent

Implement Linked List

1. Design and implement a Singly Linked List that supports the following operations:

  1. Insert a node containing value at the specified position (0-based index)
  2. Delete a node at the specified position
  3. Return the string representation of the Linked List in the format as Node1 -> Node2 -> ...-> Null
  4. If the list is empty, return "Null"

Flip the Chain

2. You are given the head of a singly linked list. Implement an efficient in-place algorithm to reverse the linked list. Return the new head of the reversed list.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters A list with 1 node: 1 → nullA long list with 10,000+ nodesA null list (head = null)
Output1 → null (no change)Full list reversed (last becomes head)null
Explanation No re-linking neededEach node’s next pointer is reassignedEdge condition, must handle gracefully

Linked Length

3. You are given the head node of a singly linked list. Design an algorithm that return the total number of nodes.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters head → null (empty list)Long list: head → 1 → 2 → ... → n → nullSingle node: head → 42 → null
Output0n (number of nodes)1
Explanation No nodes, count is zeroMust traverse all nodes to count themOnly one node, count is one

What is a loop in a linked list?

In singly linked lists, a loop occurs when a node's next pointer points to a previously visited node in the same list, rather than null. This creates a cyclic reference, leading to an infinite traversal unless handled correctly.

In industrial systems, detecting and managing such cycles is critical to avoid infinite loops, memory leaks, and application crashes, particularly in real-time systems, garbage collectors, and networked data structures.

Loop Detection

4. You are given the head node of a singly linked list. Design an algorithm that returns true if there is a cycle in the linked list. Otherwise, return false.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters 1 → 2 → 3 → null1 → 2 → 3 → 4 → 5 → 6 → 3 (cycle starts at node 3)null (empty list)
Outputfalsetruefalse
Explanation No cycle: traversal ends at nullCycle detected when fast and slow meet at node 5No nodes exist; list is empty

Midle of the List

5. Design an algorithms that takes the head of a singly linked list and returns the middle element.

If the linked list has an odd number of nodes, return the exact middle node.

If the list has an even number of nodes, return the second of the two middle nodes.

The returned node should be the actual node object, not just its value.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters A 1-node list: [7]A large even-length list: [1 → 2 → 3 → ... → 998 → 999 → 1000]A 2-node list: [10 → 20]
OutputNode with value 7Node with value 501Node with value 20
Explanation Single node is trivially the middle.Length is 1000 (even), so middle nodes are 500 and 501 → return 501.2 nodes → middle are 10 and 20 → return second (20).

Delete Target Node

6. Given the head of a singly linked list and an integer key, Design an algorithm that delete the first occurrence of the node that has this key.

The list should be modified in-place and the updated head must be returned.

If the node with the given key does not exist, return the original head unchanged.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters head = [10, 20, 30], key = 10head = [10, 20, 30, 40, 50], key = 50
head = [10], key = 15
Output[20, 30][10, 20, 30, 40][10] (unchanged)
Explanation Key found at the head. Head is updated.Key found at the last node. Full traversal.Key not found. No deletion occurs.

Insert at K-th Position

7. Given the head of a singly linked list, an integer position, and an integer value, write a function to insert a new node with the specified value at the given position.

If position is 0, insert the node at the beginning of the list.

If position is equal to the current length of the list, insert the node at the end.

If position is less than 0 or greater than the length of the list, do not perform the insertion, and return the original list.

The function should return the updated head of the list.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters head = [1, 2, 3], value = 0, pos = 0head = [1, 2, 3], value = 4, pos = 3
value = 4, pos = 3 head = [1, 2, 3], value = 5, pos = 5
Output[20, 30][1, 2, 3, 4][1, 2, 3] (unchanged)
Explanation [0, 1, 2, 3]Insert at end (position = length)Invalid position (pos > length), ignore op

Merge Sorted Lists

8. You are given the heads of two non-empty, sorted singly linked lists list1 and list2.

Design an algorithm that merges the two lists into a single, sorted linked list. The resulting list must be built by reusing and relinking the nodes from the original lists — do not allocate new nodes.

Return the head of the merged, sorted linked list.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters list1 = null
list2 = 1 → 2 → 3

list2 = 1 → 2 → 3 list1 = 1 → 3 → 5
list2 = 2 → 4 → 6
list1 = 2 → 2 → 2
list2 = 2 → 2
Output1 → 2 → 31 → 2 → 3 → 4 → 5 → 62 → 2 → 2 → 2 → 2
Explanation One list is null, so we return the other as-isAlternating values force comparisons at every stepAll values are equal; merge order determined by input

Pairwise Swap

9. Design an algorithm that takes a singly linked list and performs an in-place swap of every two neighboring nodes. The function should return the new head of the modified list.

The swapping should be done by re-linking the nodes — do not modify the node values.

If the list has an odd number of nodes, leave the last node as is.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters 1 -> 2 -> Null1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> ... -> n -> Null
Null, 1 -> Null, 1 -> 2 -> 3 -> Null
Output2 -> 1 -> NullAll adjacent pairs are swapped, e.g., 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> ...Null, 1 -> Null, 2 -> 1 -> 3 -> Null
Explanation Only one pair exists, directly swapped.Every node has a pair, all get swapped sequentially by updating .nextHandles empty list, single-node list, and last node unpaired (odd length)

- Intermediate Problems -

Break the Cycle

1. You are given the head of a singly linked list. Design an algorithm that detects whether a loop exists and remove the loop in-place by modifying the next pointer of the last node in the cycle so that it points to null.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters 1 → 2 → 3 → null1 → 2 → 3 → ... → 9999 → 10000 → 9999
1 → 2 → 3 → 4 → (back to 1)
Output1 → 2 → 3 → null1 → 2 → 3 → ... → 9999 → 10000 → null1 → 2 → 3 → 4 → null
Explanation No loop exists; return as-is.Loop is at the end; takes longest to detect and remove.Loop starts from head; early detection needed and fixed correctly.

Intersecting Linked List

2. You are given the heads of two non-empty singly linked lists. The lists may intersect at some node, forming a shared tail.

Design an algorithm that identifies the node where the intersection begins, if the two linked lists do not intersect, return null.

The lists must retain their original structure after the function returns.

A1A2 C1C2B1B2 LIST A:LIST B:Intersection Point
Parameter\ Case Best CaseWorst CaseEdge Case
Parameters headA = A1 → A2 → C1 → C2
headB = B1 → C1 → C2
headA = A1 → A2 → A3 → A4 → A5
headB = B1 → B2 → B3 → B4 → B5
headA = null, headB = B1 → B2
OR
headA = A1 → A2, headB = null
OutputReference to node C1nullnull
Explanation The lists intersect starting at node C1. The shared tail C1 → C2 is common to both.The lists do not intersect at all. All nodes are unique in memory.One or both lists are null. Hence, no intersection is possible.

Reverse K-Group Nodes

3. You are given the head of a singly linked list and an integer k. Design an algorithm to reversethe list in group of k nodes.

  • Each group of k nodes should be reversed in-place.
  • The nodes in the last group (less than k) should remain in original order.
  • Return the head of the modified list.
Parameter \ Case Best Case Worst CaseEdge Case
Input Parameters Linked List: 1→2→3→4, k = 2 Linked List: 1→2→3→4→5→6→7, k = 3Linked List: 1→2→3→4→5, k = 10
Output2→1→4→3 3→2→1→6→5→4→7 1→2→3→4→5 (no changes)
ExplanationTwo groups of size 2 reversed individually. Two full groups (3 nodes each) reversed, last node left alone No group of size 10 exists; hence, the list remains the same

Linked List Addition

4. You are given two non-empty singly linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit.

Design the algorithm that add the two numbers and return the sum as a new linked list, also in reversed order.

  • Each node in the input lists contains a single digit (0–9). You may assume the two numbers do not have leading zeros, except the number zero itself.
  • You must not modify the input lists.
  • Return a new linked list representing their sum in the same reverse format.
Input:list1 = 2 -> 4 -> 3 (represents 342)list2 = 5 -> 6 -> 4 (represents 465)Output:7 -> 0 -> 8 (represents 807)
Parameter\ Case Best CaseWorst CaseEdge Case
Parameters l1 = [0], l2 = [0] l1 = [9,9,9,9,9], l2 = [9,9,9,9,9]
One list is shorter: l1 = [2,4,3], l2 = [5,6]
Output[0][8,9,9,9,9,1][7,0,4]
Explanation Sum is 0, so result is a single nodeFull carry-over, extra node added at the end due to carryShorter list padded logically with 0s internally

Rotate Linked List

5. Given the head of a singly linked list and an integer k.

Design an algorithm that rotates the list to the right by k places. and return the new head of the rotated list.

The structure of the original list must be modified in-place.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters head = [1, 2, 3], k = 0 head = [1, 2, 3, 4, 5], k = 3
head = [1], k = 100
Output[1, 2, 3][3, 4, 5, 1, 2][1]
Explanation No rotation needed when k = 0, original list is returnedk = 8 → effective k = 3, rotate right by 3 → last 3 nodes move to frontSingle-node list remains unchanged regardless of k

Deep Copy List

6. You are given a linked list where each node has :

  • A next pointer (standard)
  • A random pointer that can point to any node in the list or be null.
(A)(B)(C)(D)(E)NULL (C)(D)NULL(A)NULL

Design an algorithm that creates a deep copy of the entire list such that:

  • The structure (next and randon) is preserved.
  • No reference from the original list is used in the new one.
  • Return the head node of the newly copied linked list.
Parameter\ Case Best CaseWorst CaseEdge Case
Parameters 1 → 2 → 3 (randoms all null) 1 → 2 → 3 → 4 → ... → N (randoms point randomly)
Empty list (null)
OutputDeep copy: 1′ → 2′ → 3′ (randoms all null)Deep copy: All N nodes copied, next and random preservednull
Explanation Minimal logic: no random pointers to trackMaximum logic: Need to copy complex random structure without sharing referencesNo nodes to copy, return null

Linked List Sorter

7. Given a singly linked list where each node contains an integer. Design an algorithm that performs in-place sorting in ascending order and returns the head of the linked list.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters 1 -> 2 -> 3 -> 4 -> null 4 -> 3 -> 2 -> 1 -> null
null (empty list) or 1 -> null
Output1 -> 2 -> 3 -> 4 -> null1 -> 2 -> 3 -> 4 -> nullnull or 1 -> null
Explanation List is already sorted; minimal work.List is reverse sorted; full reordering needed (max comparisons/swaps).Either no element or only one element; already sorted.

Flatten Multilevel List

8. Given a multilevel linked list where each node contains two pointers:

  • next: a pointer to the next node in the same level.
  • child: a pointer to a potential nested child list.
123478912 Multi Level List

Design an in-place algorithm that flatten the multilevel list into a single-level singly linked list. The flattened list should:

  • Preserve the relative order of nodes.
  • Insert child lists immediately after their parent nodes.
  • Set all child pointers in the final list to null.

Return the head of the flattened list.

123478912 Flattened List
Parameter\ Case Best CaseWorst CaseEdge Case
Parameters A flat list with no child pointers A list where every node has a child, forming a deep nested structure
Empty list or single-node list
OutputSame list unchangedFully flattened list in depth-first ordernull or single node
Explanation No flattening needed since no child existsAll child nodes are recursively inserted after parents. Deep recursion or stack usage applies.Minimal input, must handle safely without errors.

Remove Nth Node from End

9. Given the head of a singly linked list and an integer n. Design an algorithm that removes the nth node (1-based indexing) from the end of the list and return its head.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters List: 1 → 2 → 3, n = 1 List: 1 → 2 → 3 → 4 → 5, n = 5
List: 1, n = 1
Output1 → 22 → 3 → 4 → 5null
Explanation Last node (3rd from start) is removed in one pass5th from end = head, so head is removedOnly one node, and it's removed, resulting in empty list

Palindromic Linked List

10. Given a singly linked list of integers. Design an algorithm that checks whether it forms a palindrome.

Return true if it is a palindrome, otherwise return false.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters 1 → 1 → 1 → 1 1 → 2 → 3 → 2 → 1
1 node (e.g., 1), or 2 different nodes (e.g., 1 → 2)
Outputtruetruetrue for 1 node, false for non-palindrome
Explanation All values are the same. Easy match.Requires full traversal and second-half reversal and comparison.Edge case of minimal length. Algorithm must handle it gracefully without errors.