示例#1
0
from utils import ListNode, construct_linklist, pretty_linklist


def merge_two_lists(l1, l2):
    l = head = ListNode(0)
    while l1 and l2:
        if l1.val <= l2.val:
            l.next, l1 = l1, l1.next
        else:
            l.next, l2 = l2, l2.next
        l = l.next
    l.next = l1 or l2
    return head.next


if __name__ == '__main__':
    h1 = construct_linklist([1, 2, 4])
    h2 = construct_linklist([1, 3, 4])
    print(pretty_linklist(merge_two_lists(h1, h2)))
示例#2
0
            tmp = node.next
            if node.val > first_pivot.val:
                node.next = last_pivot.next
                last_pivot.next = node
            elif node.val < first_pivot.val:
                node.next = start.next
                start.next = node
            else:
                node.next = last_pivot.next
                last_pivot.next = node
                last_pivot = last_pivot.next
            node = tmp
        return [first_pivot, last_pivot]

    def qsort(start, end):
        if start.next != end:
            first_pivot, last_pivot = partition(start, end)
            qsort(start, first_pivot)
            qsort(last_pivot, end)

    head_pre = ListNode(0)
    head_pre.next = head
    qsort(head_pre, None)
    return head_pre.next


if __name__ == '__main__':
    l1 = construct_linklist([4, 2, 1, 3])
    l1 = sort_list(l1)
    print(pretty_linklist(l1))
示例#3
0
from utils import construct_linklist, pretty_linklist, ListNode


def reverse_list(head: ListNode) -> ListNode:
    prev = None
    while head:
        head.next, prev, head = prev, head, head.next
    return prev


if __name__ == '__main__':
    h1 = construct_linklist([1, 2, 3, 4, 5])
    print(pretty_linklist(h1))
    h2 = reverse_list(h1)
    print(pretty_linklist(h2))
示例#4
0
from utils import construct_linklist, pretty_linklist, ListNode


def delete_node(node: 'ListNode'):
    node.val = node.next.val
    node.next = node.next.next


if __name__ == '__main__':
    head = construct_linklist([1, 2, 3])
    print(pretty_linklist(head))
    to_del = head.next.next
    delete_node(to_del)
    print(pretty_linklist(head))

from utils import construct_linklist, ListNode


def print_list_reverse(head: ListNode) -> list:
    stack, h = [], head
    while h:
        stack.append(h.val)
        h = h.next
    return stack[::-1]


l1 = (1, 2, 3, 4, 5)
print(l1)
l1 = construct_linklist(l1)
lst1 = print_list_reverse(l1)
print(lst1)
l2 = [3, 4, 8, 2, 7]
print(l2)
l2 = construct_linklist(l2)
lst2 = print_list_reverse(l2)
print(lst2)
from utils import construct_linklist, ListNode


def print_list_reverse(head: ListNode) -> list:
    stack, h = [], head
    while h:
        stack.append(h.val)
        h = h.next
    return stack[::-1]


if __name__ == '__main__':
    l1 = (1, 2, 3, 4, 5)
    l1 = construct_linklist(l1)
    print(print_list_reverse(l1))
示例#7
0
import sys
sys.path.append("..")
from utils import construct_linklist


def get_intersection(h1, h2):
    p1, p2 = h1, h2
    while p1 is not p2:
        # NOTICE THE BUG
        # p1 = p1.next if p1.next else h2
        # p2 = p2.next if p2.next else h1
        p1 = p1.next if p1 else h2
        p2 = p2.next if p2 else h1
    return p1


if __name__ == '__main__':
    h1 = construct_linklist([4, 2, 1, 5, 5, 7])
    h2 = construct_linklist([8, 3])
    h2.next.next = h1.next.next
    intersection = h1.next.next
    print(intersection is get_intersection(h1, h2))

    h1 = construct_linklist([4, 2, 1, 5, 5, 7])
    h2 = construct_linklist([8, 3])
    print(get_intersection(h1, h2))