Skip to content

Instantly share code, notes, and snippets.

@amraks
Created February 17, 2019 19:13
Show Gist options
  • Save amraks/f6558d40054e514828874c3ade255587 to your computer and use it in GitHub Desktop.
Save amraks/f6558d40054e514828874c3ade255587 to your computer and use it in GitHub Desktop.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def swapPairs(self, head: 'ListNode') -> 'ListNode':
if not head or not head.next:
return head
cur = head
prev = None
nex = cur.next
while cur and cur.next:
nex_nex = cur.next.next
cur.next.next = cur
if prev:
prev.next = cur.next
cur.next = nex_nex
prev = cur
cur = nex_nex
return nex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment