python solution


  • 0
    D
    # 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
            """
            if head == None or head.next == None:
                return head
            i = head
            root = head.next
            while i != None:
                j = i.next
                i.next = j.next
                j.next = i
                if i.next == None:
                    return root
                elif i.next.next == None:
                    return root
                else:
                    k = i.next
                    i.next = k.next
                    i = k
                
                    
            return root
    

Log in to reply
 

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