My Python simple Solution


  • 0
        def oddEvenList(self, head):
    
            if not head: return head
            dump = ListNode(0)
            dump.next = head
            # po, pe are the pointers pointing to odd and even nodes, respectively
            po, even = head, head.next
            pe = even
            
            while pe and pe.next:
                po.next = po.next.next
                po = po.next
                if pe.next.next:
                    pe.next = pe.next.next
                    pe = pe.next
                else:
                    pe.next = None
                    
            po.next = even
            return dump.next
    

Log in to reply
 

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