0% found this document useful (0 votes)
2 views14 pages

Linked List

The document provides a collection of 30 linked list problems, ranging from easy to hard, with accompanying Python code solutions. It covers various techniques such as reversing lists, detecting cycles, merging sorted lists, and manipulating nodes. The problems are designed to enhance understanding and skills in linked list operations, commonly encountered in technical interviews.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Linked List

The document provides a collection of 30 linked list problems, ranging from easy to hard, with accompanying Python code solutions. It covers various techniques such as reversing lists, detecting cycles, merging sorted lists, and manipulating nodes. The problems are designed to enhance understanding and skills in linked list operations, commonly encountered in technical interviews.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

🔹 1.

Reverse a Linked List (Iterative – Easy)


def reverseList(head):
prev = None
curr = head
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prev

🔹 2. Detect Cycle in a Linked List (Floyd’s – Easy-Medium)


def hasCycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

🔹 3. Find Start of Cycle (Floyd's - Medium)


def detectCycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
else:
return None
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return slow

🔹 4. Merge Two Sorted Lists (Easy)


def mergeTwoLists(l1, l2):
dummy = tail = ListNode()
while l1 and l2:
if l1.val < l2.val:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next
tail.next = l1 or l2
return dummy.next

🔹 5. Remove N-th Node From End (Two Pointers – Medium)


def removeNthFromEnd(head, n):
dummy = ListNode(0, head)
slow = fast = dummy
for _ in range(n):
fast = fast.next
while fast.next:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy.next

🔹 6. Palindrome Linked List (Reverse Second Half – Medium)


def isPalindrome(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next

prev = None
while slow:
nxt = slow.next
slow.next = prev
prev = slow
slow = nxt

while prev:
if head.val != prev.val:
return False
head = head.next
prev = prev.next
return True

🔹 7. Linked List Cycle II - Return Entry Node (Medium)


Same as Q3 above

🔹 8. Add Two Numbers (Like digit by digit addition) – Medium


def addTwoNumbers(l1, l2):
dummy = curr = ListNode()
carry = 0
while l1 or l2 or carry:
v1 = l1.val if l1 else 0
v2 = l2.val if l2 else 0
carry, out = divmod(v1 + v2 + carry, 10)
curr.next = ListNode(out)
curr = curr.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next

🔹 9. Intersection of Two Linked Lists (Two Pointer – Easy)


def getIntersectionNode(headA, headB):
a, b = headA, headB
while a != b:
a = a.next if a else headB
b = b.next if b else headA
return a

🔹 10. Rotate Linked List (Medium)


def rotateRight(head, k):
if not head or not head.next: return head
old_tail = head
length = 1
while old_tail.next:
old_tail = old_tail.next
length += 1
old_tail.next = head # Make it circular
k %= length
new_tail = head
for _ in range(length - k - 1):
new_tail = new_tail.next
new_head = new_tail.next
new_tail.next = None
return new_head

🔹 11. Reverse Linked List II (Between m and n – Medium)


def reverseBetween(head, m, n):
dummy = ListNode(0)
dummy.next = head
pre = dummy
for _ in range(m-1):
pre = pre.next
curr = pre.next
for _ in range(n-m):
tmp = curr.next
curr.next = tmp.next
tmp.next = pre.next
pre.next = tmp
return dummy.next

🔹 12. Flatten a Multilevel Linked List (Hard)


def flatten(head):
if not head: return head
def dfs(node):
curr = node
last = None
while curr:
nxt = curr.next
if curr.child:
child_head = curr.child
child_tail = dfs(child_head)
curr.next = child_head
child_head.prev = curr
if nxt:
child_tail.next = nxt
nxt.prev = child_tail
curr.child = None
last = child_tail
else:
last = curr
curr = nxt
return last
dfs(head)
return head

🔹 13. Copy List with Random Pointer (HashMap – Medium)


def copyRandomList(head):
if not head: return None
old_to_new = {}
curr = head
while curr:
old_to_new[curr] = ListNode(curr.val)
curr = curr.next
curr = head
while curr:
old_to_new[curr].next = old_to_new.get(curr.next)
old_to_new[curr].random = old_to_new.get(curr.random)
curr = curr.next
return old_to_new[head]

🔹 14. Swap Nodes in Pairs (Medium)


def swapPairs(head):
dummy = ListNode(0)
dummy.next = head
prev = dummy
while head and head.next:
first = head
second = head.next

prev.next = second
first.next = second.next
second.next = first

prev = first
head = first.next
return dummy.next

🔹 15. Odd Even Linked List (Medium)


def oddEvenList(head):
if not head: return None
odd = head
even = head.next
even_head = even
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = even_head
return head

You're crushing it, Ananya! 🔥​


Here's Set 2: 15 more Linked List problems – a mix of medium and hard level questions
that are commonly asked in top companies. These involve more edge cases, pointer tricks,
and optimizations.

🔗 Linked List Interview Questions – Set 2


(Medium/Hard)

🔹 16. Reverse Nodes in k-Group (Hard)


Reverse every k nodes in the linked list.

def reverseKGroup(head, k):


def get_kth(curr, k):
while curr and k > 0:
curr = curr.next
k -= 1
return curr

dummy = ListNode(0)
dummy.next = head
group_prev = dummy

while True:
kth = get_kth(group_prev, k)
if not kth:
break
group_next = kth.next

# Reverse group
prev, curr = kth.next, group_prev.next
while curr != group_next:
tmp = curr.next
curr.next = prev
prev = curr
curr = tmp

tmp = group_prev.next
group_prev.next = kth
group_prev = tmp

return dummy.next

🔹 17. Split Linked List in Parts (Medium)


Split list into k parts as evenly as possible.

def splitListToParts(root, k):


length = 0
cur = root
while cur:
length += 1
cur = cur.next
size, extra = divmod(length, k)

parts = []
cur = root
for i in range(k):
head = cur
for j in range(size + (1 if i < extra else 0) - 1):
if cur:
cur = cur.next
if cur:
nxt = cur.next
cur.next = None
cur = nxt
parts.append(head)
return parts

🔹 18. Reorder List (Medium)


Reorder like 1 → n → 2 → n-1 → ...

def reorderList(head):
if not head:
return
# Find middle
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# Reverse second half
prev, curr = None, slow.next
slow.next = None
while curr:
tmp = curr.next
curr.next = prev
prev = curr
curr = tmp
# Merge
first, second = head, prev
while second:
tmp1, tmp2 = first.next, second.next
first.next = second
second.next = tmp1
first = tmp1
second = tmp2

🔹 19. Delete Node in a Linked List (Easy)


You're only given a node (not the head) – delete it.

def deleteNode(node):
node.val = node.next.val
node.next = node.next.next

🔹 20. Sort a Linked List (Merge Sort – Hard)


def sortList(head):
if not head or not head.next:
return head

# Split
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
mid = slow.next
slow.next = None
# Merge
left = sortList(head)
right = sortList(mid)
return merge(left, right)

def merge(l1, l2):


dummy = tail = ListNode()
while l1 and l2:
if l1.val < l2.val:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next
tail.next = l1 or l2
return dummy.next

🔹 21. Swap Nodes in Linked List (Not in pairs – Medium)


Swap nodes at kth from start and end.

def swapNodes(head, k):


fast = slow = head
for _ in range(k-1):
fast = fast.next
first = fast
while fast.next:
fast = fast.next
slow = slow.next
first.val, slow.val = slow.val, first.val
return head

🔹 22. Reverse Alternate k Nodes in a Linked List (Hard)


def reverseAlternateKNodes(head, k):
def reverse(start, k):
prev = None
curr = start
for _ in range(k):
if not curr:
return None
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prev

dummy = ListNode(0)
dummy.next = head
prev, curr = dummy, head
reverse_flag = True

while curr:
tail = curr
for _ in range(k):
if not tail:
return dummy.next
tail = tail.next
if reverse_flag:
new_head = reverse(curr, k)
prev.next = new_head
prev = curr
curr = tail
else:
prev = curr
curr = tail
reverse_flag = not reverse_flag

return dummy.next

🔹 23. Remove Duplicates from Sorted List (Easy)


def deleteDuplicates(head):
curr = head
while curr and curr.next:
if curr.val == curr.next.val:
curr.next = curr.next.next
else:
curr = curr.next
return head

🔹 24. Remove All Duplicates (Only distinct values – Medium)


def deleteDuplicates(head):
dummy = ListNode(0)
dummy.next = head
prev = dummy
while head:
if head.next and head.val == head.next.val:
while head.next and head.val == head.next.val:
head = head.next
prev.next = head.next
else:
prev = prev.next
head = head.next
return dummy.next

🔹 25. Convert Binary Number in a Linked List to Integer (Easy)


def getDecimalValue(head):
num = 0
while head:
num = num * 2 + head.val
head = head.next
return num

🔹 26. Pairwise Swap of Nodes (Medium)


def pairwiseSwap(head):
if not head or not head.next:
return head
new_head = head.next
prev = None
while head and head.next:
nxt = head.next
head.next = nxt.next
nxt.next = head
if prev:
prev.next = nxt
prev = head
head = head.next
return new_head

🔹 27. Insert into a Sorted Circular Linked List (Medium)


def insert(head, insertVal):
node = ListNode(insertVal)
if not head:
node.next = node
return node
curr = head
while True:
if curr.val <= insertVal <= curr.next.val:
break
if curr.val > curr.next.val and (insertVal >= curr.val or insertVal <= curr.next.val):
break
curr = curr.next
if curr == head:
break

node.next = curr.next
curr.next = node
return head

🔹 28. Flatten Linked List to Single Level (Medium)


(Not multilevel — just down pointers)

def flatten(head):
if not head or not head.next:
return head
head.next = flatten(head.next)
head = merge(head, head.down)
return head

def merge(a, b):


if not a: return b
if not b: return a
if a.val < b.val:
a.down = merge(a.down, b)
return a
else:
b.down = merge(a, b.down)
return b

🔹 29. Segregate Even and Odd Nodes (Maintain Order – Medium)


def segregateEvenOdd(head):
even_dummy = even_tail = ListNode(0)
odd_dummy = odd_tail = ListNode(0)
while head:
if head.val % 2 == 0:
even_tail.next = head
even_tail = even_tail.next
else:
odd_tail.next = head
odd_tail = odd_tail.next
head = head.next
even_tail.next = odd_dummy.next
odd_tail.next = None
return even_dummy.next

🔹 30. Convert Sorted List to BST (Medium-Hard)


def sortedListToBST(head):
def find_middle(start):
slow = fast = start
prev = None
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
if prev:
prev.next = None
return slow

if not head:
return None
if not head.next:
return TreeNode(head.val)
mid = find_middle(head)
root = TreeNode(mid.val)
root.left = sortedListToBST(head)
root.right = sortedListToBST(mid.next)
return root

✅ What's Next?
You're now done with 30+ high-quality Linked List questions. Next steps:

●​ Move to Trees/BSTs?​

●​ Or want a mock interview set (Mixed DSA)?​

●​ I can also give you a revision tracker sheet (Notion/Excel).​

Let me know which direction you'd like to go 💪

You might also like