TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #2957 1.14K
๐Ÿš€ Coding Interview Questions with Answers โ€” Part 3 

๐Ÿ”— Linked Lists

๐Ÿš€ 21. How do you reverse a singly linked list? 
A singly linked list can be reversed by changing the direction of pointers.

๐Ÿ”น Example 
Before: 
1 โ†’ 2 โ†’ 3 โ†’ NULL 

After: 
3 โ†’ 2 โ†’ 1 โ†’ NULL 

๐Ÿ”น Iterative Solution

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def reverse(head):
    prev = None
    current = head

    while current:
        next_node = current.next
        current.next = prev

        prev = current
        current = next_node

    return prev


๐Ÿ”น Complexity 
Time โ†’ O(n) 
Space โ†’ O(1) 

๐Ÿ”น Interview Tip 
This is one of the most important linked-list questions.

๐Ÿš€ 22. How do you detect a cycle in a linked list? 
Use Floydโ€™s Cycle Detection Algorithm. 
Also called: Tortoise and Hare Algorithm 

๐Ÿ”น Idea 
โ€ข Slow pointer moves 1 step
โ€ข Fast pointer moves 2 steps
โ€ข If they meet โ†’ cycle exists

๐Ÿ”น Python Solution

def has_cycle(head):
    slow = fast = head

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

        if slow == fast:
            return True

    return False


๐Ÿ”น Complexity 
Time โ†’ O(n) 
Space โ†’ O(1) 

๐Ÿ”น Interview Tip 
Very common interview question.

๐Ÿš€ 23. How do you find the middle node of a linked list? 
Use two pointers.

๐Ÿ”น Approach 
โ€ข Slow pointer โ†’ moves 1 step
โ€ข Fast pointer โ†’ moves 2 steps

When fast reaches end: 
slow = middle 

๐Ÿ”น Python Solution

def middle_node(head):
    slow = fast = head

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    return slow


๐Ÿ”น Complexity 
Time โ†’ O(n) 
Space โ†’ O(1) 

๐Ÿ”น Interview Tip 
Two-pointer technique is heavily used in linked lists.

๐Ÿš€ 24. How do you merge two sorted linked lists? 

๐Ÿ”น Example 
1 โ†’ 3 โ†’ 5 
2 โ†’ 4 โ†’ 6 

Merged: 
1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5 โ†’ 6 

๐Ÿ”น Python Solution

def merge_lists(l1, l2):
    dummy = Node(0)
    current = dummy

    while l1 and l2:
        if l1.data < l2.data:
            current.next = l1
            l1 = l1.next
        else:
            current.next = l2
            l2 = l2.next

        current = current.next

    current.next = l1 or l2

    return dummy.next


๐Ÿ”น Complexity 
Time โ†’ O(n + m) 
Space โ†’ O(1) 

๐Ÿ”น Interview Tip 
This problem is the base concept behind merge sort on linked lists.

๐Ÿš€ 25. How do you find and remove a duplicate in a list? 

๐Ÿ”น Using HashSet

def remove_duplicates(head):
    seen = set()

    current = head
    prev = None

    while current:
        if current.data in seen:
            prev.next = current.next
        else:
            seen.add(current.data)
            prev = current

        current = current.next

    return head


๐Ÿ”น Complexity 
Time โ†’ O(n) 
Space โ†’ O(n) 

๐Ÿ”น Without Extra Space 
Can also be solved using nested loops: O(nยฒ) 

๐Ÿ”น Interview Tip 
Interviewers may ask: Can you solve it without extra memory?

๐Ÿš€ 26. How do you implement a dummy head in linked-list problems? 
A dummy node simplifies edge cases.

๐Ÿ”น Why Useful? 
Without dummy node: Handling head insertion/deletion becomes complex 
With dummy node: Logic becomes cleaner 

๐Ÿ”น Example

dummy = Node(0)
dummy.next = head


๐Ÿ”น Use Cases 
โœ… Remove nodes 
โœ… Merge lists 
โœ… Partition lists 
โœ… Reverse sublists 

๐Ÿ”น Interview Tip 
Using dummy nodes often makes solutions cleaner and bug-free.

๐Ÿš€ 27. How do you delete a node given only that node (no head)? 
Important constraint: No access to head pointer 

๐Ÿ”น Trick 
Copy next node value into current node.

๐Ÿ”น Python Solution

def delete_node(node):
    node.data = node.next.data
    node.next = node.next.next


๐Ÿ”น Limitation 
Cannot delete last node because no next node exists.

๐Ÿ”น Interview Tip 
Classic interview trick question.
  • โค 2
More from @crackingthecodinginterview
  1. Oct 7, 2026๐Ÿš€ DSA Topics Every Programmer Should Know ๐Ÿ’ป๐Ÿ”ฅ ๐Ÿ“ฆ 1. Arrays โœ” Traversal โœ” Searching โœ” Sorโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 28, 2026Hereโ€™s a DSA problem-solving cheat sheet that will help you solve 90โ€“95% of questions thatโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’