Python Solution that beats 99.85%


  • 0
    X
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def swapPairs(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            dum = ListNode(0)
            dum.next = head
            curr = dum
            while curr.next and curr.next.next:
                temp = curr.next.next
                curr.next.next = temp.next
                temp.next = curr.next
                curr.next = temp
                curr = curr.next.next
            
            return dum.next
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.