Skip to content

Instantly share code, notes, and snippets.

@Khamies
Last active April 5, 2023 01:15
Show Gist options
  • Save Khamies/64f6cee23119553b3eff72a512db5f15 to your computer and use it in GitHub Desktop.
Save Khamies/64f6cee23119553b3eff72a512db5f15 to your computer and use it in GitHub Desktop.
def swap_node_pairs(head):
if not head or not head.next:
return head
dummy_node = ListNode()
dummy_node.next = head
prev = dummy_node
i = head
j = head.next
while prev.next and prev.next.next:
# finish here
j = i.next
# code logically starts from here
i.next = j.next
j.next = i
prev.next = j
prev = j.next
i = i.next
return dummy_node.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment