Exemplo n.º 1
0
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        dummyHead = ListNode(None, None)
        dummyHead.next = head

        prev = dummyHead
        size = 0
        while prev.next != None:
            prev.next = prev.next.next
            size += 1

        dummyHead.next = head
        prev = dummyHead

        count = 0
        while prev.next != None:
            if count == int(size - n):
                prev.next = prev.next.next
            else:
                prev = prev.next
            count += 1
        return dummyHead.next
Exemplo n.º 2
0
    def addTwoNumbers2(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        使用栈
        :param l1:
        :param l2:
        :return:
        """
        stack1, stack2 = [], []
        while l1:
            stack1.append(l1)
            l1 = l1.next
        while l2:
            stack2.append(l2)
            l2 = l2.next

        s = 0
        tail = ListNode(0)
        while stack1 or stack2:
            if stack1:
                s += stack1.pop().val
            if stack2:
                s += stack2.pop().val
            tail.val = int(s % 10)
            tmp = ListNode(int(s // 10))
            tmp.next = tail
            tail = tmp
            s /= 10
        return tail if tail.val != 0 else tail.next
Exemplo n.º 3
0
 def backtrack(head: ListNode,
               pre: Union[ListNode, None]) -> Union[ListNode, None]:
     if not head:
         return head
     if pre and pre.val == head.val or head.next and head.val == head.next.val:
         return backtrack(head.next, head)
     else:
         head.next = backtrack(head.next, head)
         return head
Exemplo n.º 4
0
 def Count(self, head, searchFor):
     dummpyHead = ListNode(None, None)
     dummpyHead.next = head
     prev = dummpyHead
     count = 0
     while prev.next != None:
         if prev.next.val == searchFor:
             count += 1
         prev = prev.next
     return count
Exemplo n.º 5
0
 def removeElements3(self, head: ListNode, val: int) -> ListNode:
     """
     recursive
     :param head:
     :param val:
     :return:
     """
     if not head:
         return head
     head.next = self.removeElements2(head.next, val)
     return head if head.val != val else head.next
Exemplo n.º 6
0
    def Count(self, head, val):
        dummpyHead = ListNode(-1)

        dummpyHead.next = head
        prev = dummpyHead
        count = 0
        while prev.next != None:
            if prev.next.val == val:
                count += 1
            prev = prev.next
        return count
Exemplo n.º 7
0
    def GetNth(self, head, index):
        count = 0
        dummpyHead = ListNode(None, None)
        dummpyHead.next = head

        prev = dummpyHead
        while prev.next != None:
            if count == index:
                return prev.next.val
            prev = prev.next
            count += 1
        return None
Exemplo n.º 8
0
 def deleteDuplicates3(self, head: ListNode) -> ListNode:
     """
     recursive
     :param head:
     :return:
     """
     if not head or not head.next:
         return head
     if head.next and head.val == head.next.val:
         while head.next and head.val == head.next.val:
             head = head.next
         return self.deleteDuplicates3(head.next)
     else:
         head.next = self.deleteDuplicates3(head.next)
     return head
Exemplo n.º 9
0
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        dummyHead = ListNode(-1)
        dummyHead.next = head

        prev = dummyHead
        while prev.next != None:
            if prev.next.val == val:
                prev.next = prev.next.next
            else:
                prev = prev.next
        return dummyHead.next
 def removeNthFromEnd(self, head, n):
     """
     :type head: ListNode
     :type n: int
     :rtype: ListNode
     """
     if head == None:
         return []
     count_down = n
     dummy = ListNode(0)
     dummy.next = head
     pointer1 = pointer2 = dummy
     pointer2_previous = dummy
     while pointer1.next:
         count_down -= 1
         if count_down < 1:
             pointer2_previous = pointer2
             pointer2 = pointer2.next
         pointer1 = pointer1.next
     pointer2_previous.next = pointer2.next
     return dummy.next
Exemplo n.º 11
0
 def reverseKGroup1(self, head: ListNode, k: int) -> ListNode:
     """
     recursive
     :param head:
     :param k:
     :return:
     """
     curr = head
     count = 0
     while curr and count < k:
         curr = curr.next
         count += 1
     if count == k:
         curr = self.reverseKGroup1(curr, k)
         while count > 0:
             tmp = head.next
             head.next = curr
             curr = head
             head = tmp
             count -= 1
         head = curr
     return head
Exemplo n.º 12
0
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None

        def getSize(head):
            counter = 0
            while head is not None:
                counter += 1
                head = head.next
            return counter

        def split(head, step):
            """
            前进 step 步 然后cut一下, 这样head就是一个长度为step的ListNode
            返回一个从head开始的第 step 个Node
            :param head:
            :param step:
            :return:
            """
            i = 1
            while i < step and head:
                head = head.next
                i += 1

            if head is None:
                return None
            # disconnect/cut
            temp, head.next = head.next, None
            return temp

        def merge(l, r, head):
            """
            将两个ListNode合并
            以 head 为头,往后将l r merge的结果填上,然后返回最后一个Node的位置
            :param l:
            :param r:
            :param head:
            :return:
            """
            cur = head
            while l and r:
                if l.val < r.val:
                    cur.next, l = l, l.next
                else:
                    cur.next, r = r, r.next
                cur = cur.next

            cur.next = l if l is not None else r
            # 让cur指向最后一个Node
            while cur.next is not None:
                cur = cur.next
            return cur

        size = getSize(head)
        bs = 1
        dummy = ListNode(0)
        dummy.next = head
        while bs < size:
            cur = dummy.next
            tail = dummy
            while cur:
                l = cur
                r = split(l, bs)
                cur = split(r, bs)
                tail = merge(l, r, tail)
            bs <<= 1
        return dummy.next