def swapPairs_bad(self, head: ListNode) -> ListNode:
        if not head.next:
            return head
        tail = ListNode()
        result = tail
        node_stack = []
        next_node = head
        while next_node:
            node_stack.append(next_node)
            next_node = next_node.next
            if len(node_stack) == 2:
                node_1 = node_stack.pop()
                node_2 = node_stack.pop()
                tail.next = node_1
                node_1.next = node_2
                node_2.next = next_node
                tail = node_2

        return result.next
Example #2
0
 def _merge(self, head1: ListNode, head2: ListNode):
     res = ListNode()
     ptr = res
     while head1 and head2:
         ptr.next = head1
         head1 = head1.next
         ptr.next.next = head2
         head2 = head2.next
         ptr = ptr.next.next
     ptr.next = head2
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        """
        Idea:
            Make a function to reverse ListNode from start to end and return the head and tail after reverse
            Call the function between k nodes
            Use iteration to prevent using extra memory
        """
        if k == 1:
            return head

        def reverse(start: ListNode,
                    end: ListNode) -> Tuple[ListNode, ListNode]:
            p = start
            pv = ListNode()
            while p and pv != end:
                ne = p.next
                p.next = pv
                pv = p
                p = ne

            return end, start

        res = ListNode()
        res.next = head
        prev = res
        ptr = head
        depth = 0
        while ptr:
            if depth == k - 1:
                n = ptr.next
                head, tail = reverse(prev.next, ptr)
                prev.next = head
                tail.next = n
                prev = ptr
                ptr = n
                depth = 0

            else:
                ptr = ptr.next
                depth += 1

        return res.next
        def reverse(start: ListNode,
                    end: ListNode) -> Tuple[ListNode, ListNode]:
            p = start
            pv = ListNode()
            while p and pv != end:
                ne = p.next
                p.next = pv
                pv = p
                p = ne

            return end, start
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        dummy = ListNode(0)  # construct a dummy node
        dummy.next = head

        pre = dummy  # set up pre and cur pointers
        cur = head
        while cur:
            if cur.next and cur.val == cur.next.val:
                # loop until cur point to the last duplicates
                while cur and cur.next and cur.val == cur.next.val:
                    cur = cur.next
                pre.next = cur.next  # propose the next for pre
                # this will be verified by next line
            else:
                pre = pre.next
            cur = cur.next
        return dummy.next
 def swapPairs(self, head):
     """
     From https://leetcode.com/problems/swap-nodes-in-pairs/discuss/11019/7-8-lines-C%2B%2B-Python-Ruby Here,
     pre is the previous node. Since the head doesn't have a previous node, I just use self instead. Again,
     a is the current node and b is the next node.
     To go from pre -> a -> b -> b.next to pre -> b -> a -> b.next, we need to change those three references.
     Instead of thinking about in what order I change them, I just change all three at once.
     """
     result = ListNode()
     pre, pre.next = result, head
     while pre.next and pre.next.next:
         a = pre.next
         b = a.next
         pre.next, b.next, a.next = b, a, b.next
         pre = a
     return result.next
Example #7
0
 def merge2List(self, head1: ListNode, head2: ListNode) -> ListNode:
     res = ListNode()
     ptr = res
     while head1 and head2:
         if head1.val < head2.val:
             ptr.next = head1
             head1 = head1.next
         else:
             ptr.next = head2
             head2 = head2.next
         ptr = ptr.next
     if head1:
         ptr.next = head1
     elif head2:
         ptr.next = head2
     return res.next
Example #8
0
 def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
     head = ListNode(0)
     ptr = head
     while l1 and l2:
         if l1.val > l2.val:
             ptr.next = l2
             l2 = l2.next
         else:
             ptr.next = l1
             l1 = l1.next
         ptr = ptr.next
     if l1:
         ptr.next = l1
     elif l2:
         ptr.next = l2
     return head.next
Example #9
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        Idea:
            1. Find out the length of each list so that we can align two linkedlist
            2. Add nodes that are in the same level together and reverse it
            3. Deal with the carry
            4. return the reversed linkedlist
        """
        if not l1 and not l2:
            return ListNode()
        if not l1:
            return l2
        if not l2:
            return l1

        # get the size of both linked list
        size_1, size_2 = 0, 0
        ptr_1, ptr_2 = l1, l2
        while ptr_1:
            size_1 += 1
            ptr_1 = ptr_1.next
        while ptr_2:
            size_2 += 1
            ptr_2 = ptr_2.next

        # add nodes that are in the same level
        res = ListNode()
        ptr_1, ptr_2, res_ptr = l1, l2, res
        while size_1 or size_2:
            if size_1 == size_2:
                res_ptr.next = ListNode(ptr_1.val + ptr_2.val)
                ptr_1 = ptr_1.next
                ptr_2 = ptr_2.next
                res_ptr = res_ptr.next
                size_1 -= 1
                size_2 -= 1
            elif size_1 > size_2:
                res_ptr.next = ListNode(ptr_1.val)
                ptr_1 = ptr_1.next
                res_ptr = res_ptr.next
                size_1 -= 1
            else:
                res_ptr.next = ListNode(ptr_2.val)
                ptr_2 = ptr_2.next
                res_ptr = res_ptr.next
                size_2 -= 1
        last = self.reverse_LinkedList(res.next)  # flip the res

        # deal with carry
        carry = 0
        last_ptr = last
        prev = None
        while last_ptr:
            last_ptr.val += carry
            carry = last_ptr.val // 10
            last_ptr.val %= 10
            prev = last_ptr
            last_ptr = last_ptr.next
        if carry:
            prev.next = ListNode(carry)

        return self.reverse_LinkedList(last)