Swap nodes in pairs
August 27, 2022
linked-listProblem URL: Swap nodes in pairs
We will create a dummy node, and set this as previous node, assing next pointer to head. Then from head, we take 2 nodes each time, swap their positions, then move the current and previous node until the end of the list. Then return the next pointer of the dummy node, which is our new head.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
dummy = ListNode(-1, head)
prev, cur = dummy, head
while cur and cur.next:
prev.next = cur.next
cur.next = cur.next.next
prev.next.next = cur
prev, cur = cur, cur.next
return dummy.next
Time Complexity: O(n)
Space Complexity: O(1)