Created
February 17, 2019 19:13
-
-
Save amraks/f6558d40054e514828874c3ade255587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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