My 52ms Python Answer with Dummy head


  • 2
    C
    • O(n) and O(1)

        dummy = ListNode(-1)
        dummy.next = head
        prev,cur = dummy,head
      
        while cur and cur.next:
            nex = cur.next.next
            prev.next = cur.next
            tmp = cur.next.next
            cur.next.next = cur
            cur.next = tmp
            prev = cur
            cur = nex
      
        return dummy.next

Log in to reply
 

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