Python Linked List Interview Questions - 15 Problems with Solutions

Linked lists test pointer manipulation, recursion, two-pointer techniques, and edge case handling - core skills for FAANG interviews.

Includes the most frequently asked problems: Reverse Linked List, Cycle Detection, Merge Two Sorted Lists, Palindrome, and more.

ListNode Class & Helpers

Python
Standard ListNode + utility functions
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def create_list(values: list[int]) -> ListNode:
    dummy = ListNode(0)
    curr = dummy
    for val in values:
        curr.next = ListNode(val)
        curr = curr.next
    return dummy.next

def print_list(head: ListNode) -> list[int]:
    result = []
    while head:
        result.append(head.val)
        head = head.next
    return result

1. Reverse Linked List (Most Asked)

Python
Iterative O(n) - 3 pointers
def reverseList(head: ListNode) -> ListNode:
    prev = None
    curr = head
    
    while curr:
        next_temp = curr.next
        curr.next = prev
        prev = curr
        curr = next_temp
    return prev

# Test
head = create_list([1,2,3,4,5])
reversed_head = reverseList(head)
print(print_list(reversed_head))  # [5,4,3,2,1]

**Time: O(n)** | **Space: O(1)** - In-place reversal

2. Merge Two Sorted Lists

Python
Dummy node + two pointers
def mergeTwoLists(list1: ListNode, list2: ListNode) -> ListNode:
    dummy = ListNode(0)
    curr = dummy
    
    while list1 and list2:
        if list1.val <= list2.val:
            curr.next = list1
            list1 = list1.next
        else:
            curr.next = list2
            list2 = list2.next
        curr = curr.next
    
    curr.next = list1 or list2
    return dummy.next

l1 = create_list([1,2,4])
l2 = create_list([1,3,4])
merged = mergeTwoLists(l1, l2)
print(print_list(merged))  # [1,1,2,3,4,4]

3. Linked List Cycle (Floyd's Algorithm)

Python
Tortoise & Hare O(n)
def hasCycle(head: ListNode) -> bool:
    slow = fast = head
    
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

# Test with cycle
head = create_list([3,2,0,-4])
head.next.next.next.next = head.next  # Create cycle
print(hasCycle(head))  # True

4. Middle of the Linked List

Python
Two pointers O(n)
def middleNode(head: ListNode) -> ListNode:
    slow = fast = head
    
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow

head = create_list([1,2,3,4,5])
print(print_list(middleNode(head))[0])  # 3

5. Remove Nth Node From End of List

Python
Two pointers gap n
def removeNthFromEnd(head: ListNode, n: int) -> ListNode:
    dummy = ListNode(0, head)
    left = dummy
    right = head
    
    # Move right n+1 steps
    while n > 0:
        right = right.next
        n -= 1
    
    # Move both until right hits end
    while right:
        left = left.next
        right = right.next
    
    left.next = left.next.next
    return dummy.next

head = create_list([1,2,3,4,5])
result = removeNthFromEnd(head, 2)
print(print_list(result))  # [1,2,3,5]

6. Add Two Numbers (Reversed)

Python
Dummy + carry
def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
    dummy = ListNode(0)
    curr = dummy
    carry = 0
    
    while l1 or l2 or carry:
        val1 = l1.val if l1 else 0
        val2 = l2.val if l2 else 0
        
        total = val1 + val2 + carry
        carry = total // 10
        curr.next = ListNode(total % 10)
        curr = curr.next
        
        l1 = l1.next if l1 else None
        l2 = l2.next if l2 else None
    
    return dummy.next

l1 = create_list([2,4,3])
l2 = create_list([5,6,4])
print(print_list(addTwoNumbers(l1, l2)))  # [7,0,8]

7. Palindrome Linked List

Python
Reverse second half
def isPalindrome(head: ListNode) -> bool:
    # Find middle
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    
    # Reverse second half
    prev = None
    while slow:
        next_temp = slow.next
        slow.next = prev
        prev = slow
        slow = next_temp
    
    # Compare first and second half
    left, right = head, prev
    while right:
        if left.val != right.val:
            return False
        left = left.next
        right = right.next
    return True

head = create_list([1,2,2,1])
print(isPalindrome(head))  # True

8. Reverse Nodes in k-Group (Hard)

Python
Count k + reverse segments
def reverseKGroup(head: ListNode, k: int) -> ListNode:
    dummy = ListNode(0, head)
    prev_group_end = dummy
    
    while True:
        group_start = prev_group_end.next
        # Check if k nodes exist
        curr = group_start
        count = 0
        while curr and count < k:
            curr = curr.next
            count += 1
        if count < k:
            return dummy.next
        
        # Reverse k nodes
        group_end = prev_group_end.next
        prev = None
        curr = group_start
        
        for _ in range(k):
            next_temp = curr.next
            curr.next = prev
            prev = curr
            curr = next_temp
        
        # Connect groups
        prev_group_end.next = prev
        prev_group_end = group_end
    
    return dummy.next

head = create_list([1,2,3,4,5])
print(print_list(reverseKGroup(head, 2)))  # [2,1,4,3,5]

Linked List Patterns

  • Dummy Node - Simplifies edge cases
  • Two Pointers - Cycle detection, middle
  • Slow/Fast Pointers - Cycle, middle
  • Reverse In-place - 3 pointers (prev, curr, next)
  • Merge Sorted - Compare + advance smaller

VS Code Debugging Setup

JSON
launch.json for linked list practice
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Linked List Practice",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    }
  ]
}

Interview Success Tips

  • Draw diagram first - show pointer movement
  • Always use dummy node for head manipulation
  • Handle edge cases: empty list, single node
  • Explain time/space complexity before coding
  • Test with 3 cases: normal, edge, empty
  • Reverse Linked List asked in 90% interviews

Master These 8 Problems

  • Reverse Linked List (Most Important)
  • Merge Two Sorted Lists
  • Cycle Detection (Floyd)
  • Middle Node
  • Remove Nth from End
  • Add Two Numbers
  • Palindrome Linked List
  • Reverse K-Group (Hard)

Practice daily with VS Code debugger (F5) - These cover 95% of linked list interview questions!