Three Pointers
206. Reverse Linked List
- Iterative
- Recursive
Interactive Visualization
- Python
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
"""Abdul Bari's solution"""
front, mid, back = head, None, None
while front:
front, mid, back = front.next, front, mid
mid.next = back
return mid
Interactive Visualization
- Python
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def recursion(curr, prev):
if not curr:
return prev
new_head = recursion(curr.next, curr)
curr.next = prev
return new_head
return recursion(head, None)
Reverse a Doubly Linked List
- Iterative
- Recursive
- Python
def reverseDLL(self, head):
front, mid, back = head, None, None
while front:
front, mid, back = front.next, front, mid
mid.next = back
mid.prev = front
return mid
- Python
def reverse(llist):
def rec(curr):
if not curr:
return None
if not curr.next:
curr.next, curr.prev = curr.prev, curr.next
return curr
new_head = rec(curr.next)
curr.next, curr.prev = curr.prev, curr.next
return new_head
return rec(llist)
Reverse both parts
- Iterative
- Python Solution
def reverse(self, head: Optional["Node"], k: int) -> Optional["Node"]:
sep = tail = new_head = None
index = 0
front, mid, back = head, None, None
while front:
if not front.next:
tail = front
if index == k - 1:
new_head = front
if index == k:
sep = front
front, mid, back = front.next, front, mid
mid.next = back
index += 1
head.next = tail
sep.next = None
return new_head
24. Swap Nodes in Pairs
Medium24. Swap Nodes in Pairs
- Iterative
- Recursive
- ⛔ Swap Values
Interactive Visualization
- Python
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
sentinel = ListNode(None, next=head)
front, mid, back = head.next, head, sentinel
while front:
mid.next = front.next
front.next = mid
back.next = front
back = mid
mid = mid.next
front = mid.next if mid else None
return sentinel.next
- Python
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
def recursion(node):
if not node or not node.next:
return node
back, front = node, node.next
back.next = recursion(front.next)
front.next = back
return front
return recursion(head)
- Python
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
curr, prev = head, None
index = 0
while curr:
if index % 2:
prev.val, curr.val = curr.val, prev.val
prev = curr
curr = curr.next
index += 1
return head
Add 1 to a Linked List Number
- Iterative (Reverse Approach)
- Recursive
- Python
class Solution:
def addOne(self, head):
def reverseList(head):
front, mid, back = head, None, None
while front:
front, mid, back = front.next, front, mid
mid.next = back
return mid
new_head = reverseList(head)
carry = 1
curr, prev = new_head, None
while curr:
carry, curr.data = divmod(curr.data + carry, 10)
curr, prev = curr.next, curr
if carry:
prev.next = Node(carry)
return reverseList(new_head)
- Python
class Solution:
def addOne(self, head):
def rec(node):
if not node:
return 1
carry = rec(node.next)
carry, node.data = divmod(node.data + carry, 10)
return carry
carry = rec(head)
if carry:
new_node = Node(carry)
new_node.next = head
head = new_node
return head