예제 #1
0
 def reverseList(self, head):
     """
     :type head: ListNode
     :rtype: ListNode
     """
     ans = None
     nextNode = head
     while nextNode:
         newNextHead = ListNode(nextNode.val)
         if ans:
             newNextHead.next = ans
         ans = newNextHead
         nextNode = nextNode.next
     return ans
예제 #2
0
    def reverse_between(self, head: ListNode, left: int,
                        right: int) -> ListNode:
        def helper(l_node: ListNode, r_node: ListNode):
            start = l_node.next
            p1, p2 = None, start
            while p2:
                if p2 == r_node:
                    break
                next_node = p2.next
                p2.next = p1
                p1 = p2
                p2 = next_node
            l_node.next.next = r_node
            l_node.next = p1

        if not head or not head.next or left == right:
            return head
        dummy = ptr = ListNode(0, head)
        i = 0
        start, end = None, None
        while ptr:
            if i == left - 1:
                start = ptr
            if i == right:
                end = ptr.next
            ptr = ptr.next
            i += 1
        helper(start, end)
        return dummy.next
예제 #3
0
def detect_cycle_1(head: ListNode) -> Optional[ListNode]:
    """
    Use extra space to record node.
    """
    if not head:
        return None
    checked = {}
    p = ListNode(0)
    p.next = head
    while p:
        p = p.next
        if not p:
            return None
        if p in checked:
            return p
        checked[p] = 1
예제 #4
0
 def test(self):
     solution = Solution()
     listNode1 = ListNode(1)
     listNode1.next = ListNode(2)
     listNode1r = ListNode(2)
     listNode1r.next = ListNode(1)
     self.assertEqual(
         assertListNodeEqual(solution.reverseList(listNode1), listNode1r),
         True)
예제 #5
0
 def removeElements(self, head: ListNode, val: int) -> ListNode:
     dummy = ListNode(next=head)
     pre, cur = dummy, head
     while cur:
         if cur.val != val:
             pre, cur = cur, cur.next
         else:
             cur = pre.next = cur.next
     return dummy.next
예제 #6
0
def merge_k_lists(lists: List[ListNode]) -> ListNode:
    ans = ListNode(0)
    mapper = collections.defaultdict(list)
    store = list()
    heapq.heapify(store)
    for node in lists:
        if node:
            heapq.heappush(store, node.val)
            mapper[node.val].append(node)
    p = ans
    while store:
        val = heapq.heappop(store)
        p.next = ListNode(val)
        p = p.next
        node = mapper[val].pop(0)
        if node.next:
            heapq.heappush(store, node.next.val)
            mapper[node.next.val].append(node.next)
    return ans.next
예제 #7
0
 def reverse_list2(self, head: ListNode) -> ListNode:
     if head is None:
         return head
     store = []
     while head is not None:
         store.append(ListNode(head.val))
         head = head.next
     for i in range(len(store) - 1, 0, -1):
         store[i].next = store[i - 1]
     return store[-1]
예제 #8
0
 def remove_nth_from_end(self, head: ListNode, n: int) -> ListNode:
     dummy = ListNode(0, head)
     fast_ptr = dummy
     for i in range(n):
         fast_ptr = fast_ptr.next
     slow_ptr = dummy
     while fast_ptr and fast_ptr.next:
         fast_ptr = fast_ptr.next
         slow_ptr = slow_ptr.next
     slow_ptr.next = slow_ptr.next.next
     return dummy.next
예제 #9
0
 def helper(l_node: ListNode, r_node: ListNode):
     start = l_node.next
     p1, p2 = None, start
     while p2:
         if p2 == r_node:
             break
         next_node = p2.next
         p2.next = p1
         p1 = p2
         p2 = next_node
     l_node.next.next = r_node
     l_node.next = p1
예제 #10
0
 def add_two_numbers(self, l1: Optional[ListNode],
                     l2: Optional[ListNode]) -> Optional[ListNode]:
     dummy = ListNode(0)
     cur = dummy
     add = 0
     while l1 and l2:
         add, val = divmod(l1.val + l2.val + add, 10)
         cur.next = ListNode(val)
         cur = cur.next
         l1 = l1.next
         l2 = l2.next
     l3 = l1 if l1 else l2
     while l3:
         if add == 0:
             cur.next = l3
             break
         add, val = divmod(l3.val + add, 10)
         cur.next = ListNode(val)
         cur = cur.next
         l3 = l3.next
     if add:
         cur.next = ListNode(add)
     return dummy.next
예제 #11
0
 def test(self):
     solution = Solution()
     listNode1 = ListNode(1)
     listNode1.next = ListNode(2)
     currentNode1 = ListNode(3)
     listNode1.next.next = currentNode1
     listNode1.next.next.next = ListNode(4)
     solution.deleteNode(currentNode1)
     self.assertEqual(listNode1.next.next.val, 4)
예제 #12
0
 def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
     dummy = ListNode(0)
     ptr = dummy
     p1, p2 = list1, list2
     while p1 and p2:
         if p1.val <= p2.val:
             ptr.next = p1
             p1 = p1.next
         else:
             ptr.next = p2
             p2 = p2.next
         ptr = ptr.next
     if p1:
         ptr.next = p1
     elif p2:
         ptr.next = p2
     return dummy.next
예제 #13
0
 def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
     if not head:
         return head
     dummy = ListNode(head.val - 1)
     pre = dummy
     cur = head
     while cur:
         if cur.next:
             if cur.val != pre.val and cur.val != cur.next.val:
                 pre.next = cur
                 cur = cur.next
                 pre = pre.next
                 pre.next = None
             else:
                 val = cur.val
                 while cur and cur.val == val:
                     cur = cur.next
         else:
             pre.next = cur
             cur = cur.next
     return dummy.next
예제 #14
0
 def test(self):
     listNode1 = ListNode(1)
     solution = Solution(listNode1)
     self.assertEqual(solution.getRandom(), 1)
 def test(self):
     solution = Solution()
     listNode1 = ListNode(1)
     listNode1.next = ListNode(2)
     listNode1.next.next = ListNode(3)
     listNode11 = ListNode(1)
     listNode12 = ListNode(2)
     listNode13 = ListNode(3)
     self.assertEqual(
         assertListNodeArrayEqual(
             solution.splitListToParts(listNode1, 5),
             [listNode11, listNode12, listNode13, None, None]), True)
     listNode2 = ListNode(1)
     listNode2.next = ListNode(2)
     listNode2.next.next = ListNode(3)
     listNode2.next.next.next = ListNode(4)
     listNode2.next.next.next.next = ListNode(5)
     listNode2.next.next.next.next.next = ListNode(6)
     listNode2.next.next.next.next.next.next = ListNode(7)
     listNode2.next.next.next.next.next.next.next = ListNode(8)
     listNode2.next.next.next.next.next.next.next.next = ListNode(9)
     listNode2.next.next.next.next.next.next.next.next.next = ListNode(10)
     listNode21 = ListNode(1)
     listNode21.next = ListNode(2)
     listNode21.next.next = ListNode(3)
     listNode21.next.next.next = ListNode(4)
     listNode22 = ListNode(5)
     listNode22.next = ListNode(6)
     listNode22.next.next = ListNode(7)
     listNode23 = ListNode(8)
     listNode23.next = ListNode(9)
     listNode23.next.next = ListNode(10)
     self.assertEqual(
         assertListNodeArrayEqual(solution.splitListToParts(listNode2, 3),
                                  [listNode21, listNode22, listNode23]),
         True)
     self.assertEqual(
         assertListNodeArrayEqual(solution.splitListToParts(None, 3),
                                  [None, None, None]), True)
예제 #16
0
    def list2node(l: List[int]) -> ListNode:
        node = None
        for ele in l:
            node = ListNode(ele)

        return node