Easy to understand python solution


  • 0
    T
    class Solution(object):
        def swapPairs(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            st = ListNode(0)
            st.next = head
            h = st
            if not head:
                return head
            while st:
                if st.next and st.next.next:
                    nx = st.next
                    st.next = st.next.next
                    nx.next = nx.next.next
                    st.next.next = nx
                else:
                    break
                st = st.next.next
            return h.next
    

Log in to reply
 

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