예제 #1
0
def rehashing(
        hash_table: List[Optional[ListNode]]) -> List[Optional[ListNode]]:
    nums: List[int] = []
    old_capacity = 0
    elements_count = 0
    for each in hash_table:
        old_capacity += 1
        if each is None:
            continue
        curr_node = each
        while curr_node is not None:
            elements_count += 1
            # nums.append(curr_node.val)
            # 本题要求按栈的顺序遍历旧哈希表的数据,实际上顺序是无所谓的
            nums.insert(0, curr_node.val)
            curr_node = curr_node.next
    # print(elements_count, old_capacity, nums)
    if elements_count * 10 < old_capacity:
        return hash_table
    new_capacity = old_capacity * 2
    new_hash_table: List[Optional[ListNode]] = [None] * new_capacity
    for num in nums:
        new_index = num % new_capacity
        if new_hash_table[new_index] is None:
            new_hash_table[new_index] = ListNode(num)
        else:
            new_node = ListNode(num)
            new_node.next = new_hash_table[new_index]
            new_hash_table[new_index] = new_node
    return new_hash_table
예제 #2
0
 def test_1(self):
     # Input: 1->2->3->4->5->NULL
     # Output: 5->4->3->2->1->NULL
     head = ListNode.build([1, 2, 3, 4, 5])
     expected = '5 -> 4 -> 3 -> 2 -> 1'
     result = self.func.reverseList(head)
     self.assertEqual(result.val, 5)
     self.assertEqual(result.next.val, 4)
     self.assertEqual(ListNode.stringify(result), expected)
예제 #3
0
 def test_1(self):
     lists = [
         ListNode.build([1, 4, 5]),
         ListNode.build([1, 3, 4]),
         ListNode.build([2, 6])
     ]
     expected = '1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6'
     result = self.func.mergeKLists(lists)
     self.assertEqual(ListNode.stringify(result), expected)
 def test_2(self):
     a = ListNode(9)
     a.next = ListNode(8)
     b = ListNode(1)
     head = self.func.addTwoNumbers(a, b)
     cur, result = head, ''
     while cur:
         result = str(cur.val) + result
         cur = cur.next
     self.assertEqual(result, "90")
 def test_2(self):
     a = ListNode(2)
     a.next = ListNode(6)
     a.next.next = ListNode(4)
     b = ListNode(1)
     b.next = ListNode(5)
     result = self.func.getIntersectionNode(a, b)
     self.assertIsNone(result)
예제 #6
0
 def even_odd_merge(self,L:ListNode)->Optional[ListNode]:
     if L is None:
         return L
     even_dummy_head, odd_dummy_head = ListNode(0), ListNode(0)
     tails, turn = [even_dummy_head,odd_dummy_head] , 0
     while L:
         tails[turn].next = L
         L = next
         tails[turn] = tails[turn].next
         turn ^= 1 # Alternate between even and odd.
     tails[0].next = None
     tails[1].next = odd_dummy_head.next
     return even_dummy_head.next
예제 #7
0
 def test_rehashing(self):
     for input_list_str, expected_list_str in self.TEST_CASES:
         input_hash_table = [ListNode.from_str(s) for s in input_list_str]
         output_hash_table = rehashing(input_hash_table)
         output_list_str = [
             "" if node is None else str(node) for node in output_hash_table
         ]
         self.assertListEqual(expected_list_str, output_list_str)
 def test_1(self):
     '''
     Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
     Output: 7 -> 0 -> 8 --- 342 + 465 = 807
     '''
     a = ListNode(2)
     a.next = ListNode(4)
     a.next.next = ListNode(3)
     b = ListNode(5)
     b.next = ListNode(6)
     b.next.next = ListNode(4)
     head = self.func.addTwoNumbers(a, b)
     cur, result = head, ''
     while cur:
         result = str(cur.val) + result
         cur = cur.next
     self.assertEqual(result, "807")
    def reverse_sublist(self, L: ListNode, start: int,
                        finish: int) -> Optional[ListNode]:
        dummy_head = sublist_head = ListNode(0, L)
        # Finds the start of the sublist.
        for _ in range(1, start):
            sublist_head = sublist_head.next

        # Reverses sublist.
        sublist_iter = sublist_head.next
        for _ in range(finish - start):
            temp = sublist_iter.next
            sublist_iter.next, temp.next, sublist_head.next = temp.next, sublist_head.next, temp
        return dummy_head.next
    def merge_two_sorted_lists(self, L1: Optional[ListNode],
                               L2: Optional[ListNode]) -> Optional[ListNode]:
        dummy_head = tail = ListNode()

        while L1 and L2:
            if L1.data <= L2.data:
                tail.next, L1 = L1, L1.next
            else:
                tail.next, L2 = L2, L2.next
            tail = tail.next

        tail.next = L1 or L2
        return dummy_head.next
예제 #11
0
    def remove_kth_last(self, L: ListNode, k: int) -> Optional[ListNode]:
        dummy_head = ListNode(0, L)

        first = dummy_head.next
        for _ in range(k):
            print("go")
            first = first.next

        second = dummy_head
        print("first: ", first.data, " second:", second.data)
        while first:
            first, second = first.next, second.next

        second.next = second.next.next
        return dummy_head.next
예제 #12
0
    def merge_two_lists(l1: ListNode, l2: ListNode) -> ListNode:
        new_ln_head = ListNode(0)
        curr = new_ln_head
        while l1 and l2:
            if l1.val < l2.val:
                curr.next = l1
                l1 = l1.next
            else:
                curr.next = l2
                l2 = l2.next
            curr = curr.next

        if l1:
            curr.next = l1
        else:
            curr.next = l2

        return new_ln_head.next
예제 #13
0
    def heapq_solution(lists: List[ListNode]) -> Optional[ListNode]:
        k = len(lists)
        if k == 0:
            return None
        # 长度固定为k的优先队列
        heap = []
        for i in range(k):
            if lists[i] is None:
                continue
            heapq.heappush(heap, NodeWrapper(lists[i]))

        dummy = ListNode(0)
        curr = dummy
        while heap:
            wrapper = heapq.heappop(heap)
            curr.next = wrapper.inner
            curr = curr.next
            if wrapper.inner.next is not None:
                wrapper.inner = wrapper.inner.next
                heapq.heappush(heap, wrapper)
        return dummy.next
 def test_1(self):
     c = ListNode(8)
     c.next = ListNode(4)
     c.next.next = ListNode(5)
     a = ListNode(4)
     a.next = ListNode(1)
     a.next.next = c
     b = ListNode(5)
     b.next = ListNode(0)
     b.next.next = ListNode(1)
     b.next.next.next = c
     result = self.func.getIntersectionNode(a, b)
     self.assertEqual(result, c)
예제 #15
0
 def test(self):
     for l1, l2, output in self.MERGE_TWO_LISTS_CASES:
         self.assertEqual(
             output,
             self.merge_two_lists(ListNode.from_str(l1),
                                  ListNode.from_str(l2)).__str__())
예제 #16
0
 def test_1(self):
     head = ListNode.build([1, 2, 3, 4, 5])
     n = 2
     expected = '1 -> 2 -> 3 -> 5'
     result = self.func.removeNthFromEnd(head, n)
     self.assertEqual(ListNode.stringify(result), expected)
예제 #17
0
 def test_2(self):
     head = ListNode.build([1, 2, 3, 4, 5])
     expected = '1 -> 5 -> 2 -> 4 -> 3'
     self.func.reorderList(head)
     self.assertEqual(ListNode.stringify(head), expected)
예제 #18
0
        slow = fast = L
        L1 = LinkedList()
        while fast and fast.next:
            fast, slow = fast.next.next, slow.next
            #Compares the first half and the reversed second half lists.
            first_half_iter, second_half_iter = L, L1.reverse(slow)
            while second_half_iter and first_half_iter:
                if second_half_iter.data != first_half_iter.data:
                    return False
                second_half_iter, first_half_iter = (second_half_iter.next,
                                                     first_half_iter.next)
        return True


if __name__ == "__main__":
    mySolution = Solution()
    myNode1 = ListNode(0)
    myNode2 = ListNode(1)
    myNode3 = ListNode(2)
    myNode4 = ListNode(1)
    myNode5 = ListNode(0)
    L1 = LinkedList()
    L1.insert_after(myNode1, myNode2)
    L1.insert_after(myNode2, myNode3)
    L1.insert_after(myNode3, myNode4)
    L1.insert_after(myNode4, myNode5)
    L1.print_list(myNode1)
    # L1.print_list(L1.reverse(myNode1))
    L1.print_list(myNode1)
    print(mySolution.is_linked_list_a_palindrome(myNode1))
예제 #19
0
 def test_2(self):
     head = ListNode.build([-1, 5, 3, 4, 0])
     expected = "-1 -> 0 -> 3 -> 4 -> 5"
     result = self.func.sortList(head)
     self.assertEqual(ListNode.stringify(result), expected)
예제 #20
0
 def test_1(self):
     head = ListNode.build([4, 2, 1, 3])
     expected = "1 -> 2 -> 3 -> 4"
     result = self.func.sortList(head)
     self.assertEqual(ListNode.stringify(result), expected)
 def test_1(self):
     head = ListNode.build([1, 2])
     self.assertFalse(self.func.isPalindrome(head))
 def test_2(self):
     head = ListNode.build([1, 2, 3, 4, 5, 6])
     expected = 4
     result = self.func(head)
     self.assertEqual(result.val, expected)
 def test_2(self):
     head = ListNode.build([1, 2, 2, 1])
     self.assertTrue(self.func.isPalindrome(head))
    def reverse_sublist(self, L: ListNode, start: int,
                        finish: int) -> Optional[ListNode]:
        dummy_head = sublist_head = ListNode(0, L)
        # Finds the start of the sublist.
        for _ in range(1, start):
            sublist_head = sublist_head.next

        # Reverses sublist.
        sublist_iter = sublist_head.next
        for _ in range(finish - start):
            temp = sublist_iter.next
            sublist_iter.next, temp.next, sublist_head.next = temp.next, sublist_head.next, temp
        return dummy_head.next


if __name__ == "__main__":
    mySolution = Solution()

    myNode1 = ListNode(11)
    myNode2 = ListNode(3)
    myNode3 = ListNode(5)
    myNode4 = ListNode(7)
    myNode5 = ListNode(2)
    L1 = LinkedList()
    L1.insert_after(myNode1, myNode2)
    L1.insert_after(myNode2, myNode3)
    L1.insert_after(myNode3, myNode4)
    L1.insert_after(myNode4, myNode5)
    L1.print_list(myNode1)
    L1.print_list(mySolution.reverse_sublist(myNode1, 2, 4))
 def test_1(self):
     l1 = ListNode.build([1, 2, 4])
     l2 = ListNode.build([1, 3, 4])
     expected = '1 -> 1 -> 2 -> 3 -> 4 -> 4'
     result = self.func(l1, l2)
     self.assertEquals(ListNode.stringify(result), expected)