コード例 #1
0
'''

import sys

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


class Solution:
    def selectmid(self, head):
        list_len = 0
        temp_list = []
        p = head
        while p:
            list_len = list_len + 1
            temp_list.append(p)
            p = p.next
        return temp_list[int((list_len) / 2)].val


if __name__ == "__main__":

    solution = Solution()

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

    print(res)
コード例 #2
0
ファイル: demo.py プロジェクト: km1994/leetcode
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)
コード例 #3
0
ファイル: demo.py プロジェクト: km1994/leetcode
            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)