Пример #1
0
    def sortList(self, head: ListNode) -> ListNode:
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next: return head

        total_length = self.get_size(head)
        dummy = ListNode(0)
        dummy.next = head
        size = 1

        while size < total_length:
            dummy_start = dummy
            start = dummy.next
            while start:
                left = start
                right = self.split(left,
                                   size)  # start from left, cut with size=size
                start = self.split(
                    right, size)  # start from right, cut with size=size
                dummy_start = self.merge(
                    left, right,
                    dummy_start)  # returned tail = next dummy_start
            size *= 2
        return dummy.next
Пример #2
0
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head

        # dummy node acts as a prevNode for the head node
        # of the list and hence and hence stores pointer to the head node.
        dummy = ListNode(-1)
        dummy.next = head

        prev_node = dummy

        while head and head.next:
            # nodes to be swapped
            first = head
            second = head.next

            # swapping
            prev_node.next = second
            first.next = second.next
            second.next = first

            # reinit the head and prev_node for the next swap
            prev_node = first
            head = first.next

        # return new head node
        return dummy.next
    def insert(self, head: 'Node', insertVal: int) -> 'Node':
        if not head:
            head = Node(insertVal)
            head.next = head
            return head

        curr = head

        while True:
            # found place to insert, e.g. 9 between 8 and 10
            if curr.val <= insertVal <= curr.next.val:
                break

            # last element
            if curr.val > curr.next.val:
                # greater than tail (max num)
                if curr.val <= insertVal >= curr.next.val:
                    break
                # lower than head (min num)
                elif curr.val >= insertVal <= curr.next.val:
                    break

            # all elements are equal
            if curr.next == head:
                break
            curr = curr.next

        new_node = Node(insertVal)
        next_node = curr.next
        curr.next = new_node
        new_node.next = next_node
        return head
Пример #4
0
    def sortList(self, head: ListNode) -> ListNode:
        values = list()
        node = head
        while node:
            values.append(node.val)
            node = node.next
        values.sort()

        dummy_node = ListNode()
        prev = dummy_node
        for v in values:
            prev.next = ListNode(val=v)
            prev = prev.next
        return dummy_node.next
Пример #5
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        l1_nums, l2_nums = [], []
        while l1:
            l1_nums.append(l1.val)
            l1 = l1.next
        while l2:
            l2_nums.append(l2.val)
            l2 = l2.next

        prev, head = None, None
        carry = 0
        while l1_nums or l2_nums or carry:
            a_val = l1_nums.pop() if l1_nums else 0
            b_val = l2_nums.pop() if l2_nums else 0
            c_sum = a_val + b_val + carry

            new_node = ListNode(c_sum % 10)  # num at node should be <= 9
            carry = c_sum // 10
            if not prev:
                prev = new_node
                head = prev
            else:
                prev.next = new_node
                prev = new_node
        sum_head = self.reverse_linked_list(head)
        return sum_head
Пример #6
0
 def swapPairs(self, head: ListNode) -> ListNode:
     if head and head.next:
         temp = head.next
         head.next, temp.next = self.swapPairs(temp.next), head
         return temp
     else:
         return head
Пример #7
0
 def reverseList(self, head: ListNode) -> ListNode:
     if head is None or head.next is None:
         return head
     p = self.reverseList(head.next)
     head.next.next = head
     head.next = None
     return p
Пример #8
0
 def create_linked_list() -> ListNode:
     ln1 = ListNode(1)
     ln2 = ListNode(2)
     ln3 = ListNode(3)
     ln1.next = ln2
     ln2.next = ln3
     return ln1
Пример #9
0
    def reverseList(self, head: ListNode) -> ListNode:
        if not head:
            return

        # head is the original head and curr_head is the new head
        curr_head = head
        while head and head.next:
            node = head.next  # remember node
            head.next = node.next  # jump original head link over node
            node.next = curr_head
            curr_head = node  # set new head
        return curr_head
Пример #10
0
 def add_two_numbers(self, l1, l2):
     """
     :type l1: ListNode
     :type l2: ListNode
     :rtype: ListNode
     """
     res = home = ListNode(0)
     carry = 0
     while l1 or l2 or carry:
         v1 = v2 = 0
         if l1:
             v1 = l1.val
             l1 = l1.next
         if l2:
             v2 = l2.val
             l2 = l2.next
         val_sum = v1 + v2 + carry
         val, carry = val_sum % 10, int(val_sum / 10)
         res.next = ListNode(val)
         res = res.next
     return home.next
Пример #11
0
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        prevhead = ListNode(-1)

        prev = prevhead
        while l1 and l2:
            if l1.val <= l2.val:
                prev.next = l1
                l1 = l1.next
            else:
                prev.next = l2
                l2 = l2.next
            prev = prev.next

        prev.next = l1 or l2
        return prevhead.next
Пример #12
0
def merge_two_lists(l1, l2):
    cur = head = ListNode(0)
    while l1 and l2:
        if l1.val <= l2.val:
            cur.next = l1
            l1 = l1.next
        else:
            cur.next = l2
            l2 = l2.next
        cur = cur.next
    if l1:
        cur.next = l1
    if l2:
        cur.next = l2
    return head.next
Пример #13
0
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        list_values = list()
        while l1:
            list_values.append(l1.val)
            l1 = l1.next
        while l2:
            list_values.append(l2.val)
            l2 = l2.next
        list_values.sort()

        head = None
        prev = None
        for v in list_values:
            node = ListNode(v)
            if not head:
                head = prev = node
            else:
                prev.next = node  # make link to the new node
                prev = node  # remember the new node for next iteration
        return head
Пример #14
0
def rotate_right(head, k):
    cur = head
    n = 0
    while cur:
        n += 1
        cur = cur.next
    if n == 0:
        return head
    k = k % n
    if k == 0:
        return head
    fast = slow = home = ListNode(0)
    fast.next = slow.next = home.next = head
    for i in range(k):
        fast = fast.next
    while fast.next:
        fast = fast.next
        slow = slow.next
    home.next = slow.next
    slow.next = None
    fast.next = head
    return home.next