Exemplo n.º 1
0
from T0_ListDifine.tools import arr2List, print_list, ListNode


class Solution:
    def removeDuplicateNodes(self, head):
        if not head:
            return head
        val_set = set()
        val_set.add(head.val)
        p = head
        while p.next:
            q = p.next
            if q.val not in val_set:
                val_set.add(q.val)
                p = p.next
            else:
                p.next = p.next.next
        return head


if __name__ == "__main__":

    solution = Solution()

    print("----------1-----------")
    l1 = arr2List([1, 2, 3, 3, 2, 1])
    print_list(l1)
    res = solution.removeDuplicateNodes(l1)

    print_list(res)
Exemplo n.º 2
0
import sys
sys.path.append("..")  # 这句是为了导入_config

from T0_ListDifine.tools import arr2List, print_list, ListNode


class Solution:
    def binaryTreePaths(self, root: TreeNode) -> List[str]:
        res = []

        def backtrack(root, path):
            if root:
                path = path + str(root.val)
                if not root.left and not root.right:
                    res.append(path)
                path = path + "->"
                backtrack(root.left, path)
                backtrack(root.right, path)

        backtrack(root, "")
        return res


if __name__ == "__main__":
    l1 = arr2List([1, 2, 4, 5, 6, 8, 9])
    print_list(l1)
    solution = Solution()
    res = solution.binaryTreePaths(l1)

    print_list(res)
Exemplo n.º 3
0
                p = p.next
            else:
                q.next = h
                q = q.next
            h = h.next
            num = num + 1
        p.next = q_head.next
        q.next = None
        return p_head.next

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

    print("----------1-----------")
    l1 = arr2List([1,2,3,4,5,6,7,8,9])
    print_list(l1)
    res = solution.oddEvenList(l1)

    print_list(res)
    






        


  
Exemplo n.º 4
0
'''

import sys
sys.path.append("..")  # 这句是为了导入_config
from T0_ListDifine.tools import arr2List, print_list, ListNode


class Solution:
    def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
        p = head
        while p and k > 0:
            p = p.next
            k = k - 1
        if not p:
            return head if k == 0 else None
        q = head
        while p:
            p = p.next
            q = q.next
        return q


if __name__ == "__main__":
    solution = Solution()
    print("----------1-----------")
    l1 = arr2List([1, 1, 2])
    k = 2
    print_list(l1)
    res = solution.getKthFromEnd(l1, k)
    print_list(res)
Exemplo n.º 5
0
                l2 = l2.next
            else:
                h.next = l1
                l1 = l1.next
            h = h.next
        
        if l1:
            h.next = l1
            

        if l2:
            h.next = l2 
        return head.next

if __name__ == "__main__":
    l1 = arr2List([1,2,4,5,6,8,9])
    l2 = arr2List([1,3,4])
    print_list(l1)
    print_list(l2)
    solution = Solution()
    res = solution.mergeTwoLists(l1,l2)

    print_list(res)
    






        
Exemplo n.º 6
0
        pre_head = ListNode(0)
        pre_head.next = head
        # step 3:初始化 位置
        last = head  # 已排好序的链表的最后一个节点
        curr = head.next  # 当前节点
        while curr:
            # step 4:如果已排好序的链表最后一个节点值 小于 当前节点的值 的情况
            if last.val <= curr.val:
                last = last.next
            # step 5:如果已排好序的链表最后一个节点值 大于 当前节点的值 的情况
            else:
                # step 6:寻找 last 的 前驱节点
                pre = pre_head
                while pre.next.val <= curr.val:
                    pre = pre.next
                # step 7:核心,改变顺序,建议纸上画一下
                pre.next = curr.next
                curr.next = pre.next
                pre.next = curr
            # step 8:curr 指向 last 后面 一个节点
            curr = last.next
        return pre_head.next


if __name__ == "__main__":
    l1 = arr2List([4, 2, 1, 3])
    print_list(l1)
    solution = Solution()
    res = solution.insertionSortList(l1)
    print_list(res)
Exemplo n.º 7
0
                l1 = l1.next
            h = h.next
        if l1:
            h.next = l1
        if l2:
            h.next = l2 
        return head.next


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

    print("----------1-----------")
    lists = []
    l1 = arr2List([1,3,5,6])
    l2 = arr2List([2,4,9])
    l3 = arr2List([7,8])
    print_list(l1)
    print_list(l2)
    print_list(l3)
    lists.append(l1)
    lists.append(l2)
    lists.append(l3)
    res = solution.mergeKLists(lists)

    print_list(res)
    


Exemplo n.º 8
0
                temp_set.add(p.next.val)
            else:
                q = p.next
                p.next = q.next
                del q
            p = p.next


        return head

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

    print("----------1-----------")
    l1 = arr2List([1,1,2,3,4,5,6,6,7,8,9,10])
    print_list(l1)
    res = solution.removeRepeat(l1)

    print_list(res)