Exemple #1
0
class LinklistStack(object):
    def __init__(self):
        self._data = LinkList()

    def __len__(self):
        return self._data.size

    def is_empty(self):
        return self._data.size == 0

    def push(self, value):
        return self._data.add_first(value)

    def pop(self):
        if self.is_empty():
            raise ValueError('Stack is empty')
        return self._data.remove_first()

    def top(self):
        if self.is_empty():
            raise ValueError('Stack is empty')
        return self._data.head.next.value

    def printstack(self):
        return self._data.print_list()
Exemple #2
0
def half_split(llist):
    assert llist.head is not None and llist.head.next is not None
    fast = llist.head
    slow = llist.head

    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow = slow.next

    new_list = LinkList()
    new_list.head.next = slow.next
    new_list.size = llist.size // 2
    slow.next = None
    llist.size -= llist.size // 2

    return llist, new_list
Exemple #3
0
def merge_list2(list_a, list_b):
    a = list_a.head.next
    b = list_b.head.next
    result = c = Node()
    while a != None and b != None:
        if a.value <= b.value:
            c.next = a
            a = a.next
        else:
            c.next = b
            b = b.next
        c = c.next
    c.next = a or b
    ll = LinkList()
    ll.head.next = result.next
    return ll
Exemple #4
0
 def __init__(self):
     self._data = LinkList()
Exemple #5
0
    while head is not None and head.next is not None:
        if head.value == head.next.value:
            while head is not None and head.next is not None and head.value == head.next.value:
                head = head.next
            head = head.next
            pre.next = head
        else:
            pre = pre.next
            head = head.next

    return dummy


if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(1)
    ll.add_last(1)
    ll.add_last(1)
    ll.add_last(2)
    ll.add_last(2)
    ll.add_last(3)
    ll.add_last(4)
    ll.add_last(4)
    ll.add_last(5)
    ll.add_last(6)
    ll.add_last(7)
    ll.add_last(7)
    ll.print_list()
    print()
    result = delet_duplicate(ll)
Exemple #6
0
        pre = cur
        cur = temp
    alist.head.next = pre


def reverse_recursion(node):
    if node.next is None:
        return node
    head = reverse_recursion(node.next)
    node.next.next = node
    node.next = None
    return head


if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(1)
    ll.add_last(2)
    ll.add_last(3)
    ll.add_last(4)
    ll.print_list()
    print()
    reverse_l(ll)
    ll.print_list()
    print()
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node1.next = node2
    node2.next = node3
Exemple #7
0
        fast = fast.next
        n -= 1

    while fast != None:
        fast = fast.next
        slow = slow.next
    if slow.next != None:
        slow.value = slow.next.value
        slow.next = slow.next.next
        llist.size -= 1
    else:
        slow.value = None
        slow.next = None
        llist.size -= 1

    return llist

if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(1)
    ll.add_last(2)
    ll.add_last(3)
    ll.add_last(4)
    ll.print_list()
    print()
    ll.length()
    print()
    result = dele_reverse(ll,4)
    result.print_list()
    print()
    ll.length()
Exemple #8
0

def merge_recursive(a, b):
    if not a or not b:
        return a or b

    if a.value < b.value:
        a.next = merge_recursive(a.next, b)
        return a
    else:
        b.next = merge_recursive(a, b.next)
        return b


if __name__ == '__main__':
    ll_a = LinkList()
    ll_a.add_last(1)
    ll_a.add_last(3)
    ll_a.add_last(5)
    ll_a.add_last(7)
    ll_b = LinkList()
    ll_b.add_last(2)
    ll_b.add_last(4)
    ll_b.add_last(5)
    ll_b.add_last(8)
    ll_b.add_last(10)
    ll_b.add_last(12)

    node1 = Node(1)
    node2 = Node(3)
    ndoe3 = Node(5)
Exemple #9
0

def merge(alist):
    dummy = Node(0)
    cur = alist.head.next

    while cur is not None:
        pre = dummy
        while pre.next is not None and pre.next.value < cur.value:
            pre = pre.next
        temp = cur.next
        cur.next = pre.next
        pre.next = cur
        cur = temp
    return dummy.next


if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(9)
    ll.add_last(2)
    ll.add_last(7)
    ll.add_last(8)
    ll.add_last(5)
    ll.add_last(1)
    ll.print_list()
    result = merge(ll)
    print()
    l2 = LinkList()
    l2.head.next = result
    l2.print_list()
Exemple #10
0
def merge(left, right):
    dummy = head = Node(0)
    while left and right:
        if left.value < right.value:
            head.next = left
            left = left.next
        else:
            head.next = right
            right = right.next
        head = head.next
    if left != None:
        head.next = left
    if right != None:
        head.next =right
    return dummy.next

if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(8)
    ll.add_last(3)
    ll.add_last(7)
    ll.add_last(2)
    ll.add_last(1)
    ll.add_last(12)
    ll.add_last(9)

    result = sort_merge(ll)
    l2 = LinkList()
    l2.head.next = result
    l2.print_list()
Exemple #11
0
from linklist_f import LinkList

def find_mid(llist):
    assert llist.head is not None and llist.head.next is not None
    fast = llist.head
    slow = llist.head
    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow = slow.next
    return slow.value


if __name__ == '__main__':
    ll = LinkList()
    ll.add_first(1)
    ll.add_last(2)
    ll.add_last(3)
    ll.print_list()
    if ll.head is not None:
        print('right')

    result = find_mid(ll)
    print(result)
Exemple #12
0
def reverse_m2n(alist, m, n):
    pre = alist.head
    for _ in range(m - 1):
        pre = pre.next
    cur = pre.next

    result = None
    for _ in range(n - m + 1):
        temp = cur.next
        cur.next = result
        result = cur
        cur = temp
    pre.next.next = cur
    pre.next = result


if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(1)
    ll.add_last(2)
    ll.add_last(3)
    ll.add_last(4)
    ll.add_last(5)
    ll.add_last(6)
    ll.add_last(7)
    ll.add_last(8)
    ll.print_list()
    print()
    reverse_m2n(ll, 4, 7)
    ll.print_list()
Exemple #13
0
def delet_repeall(alist):
    head = alist.head.next

    while head.next is not None:
        if head.value == head.next.value:
            head = head.next
        elif head.next.value == head.next.next.value:
            head = head.next
        else:
            head = head.next
            break

    return head

if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(1)
    ll.add_last(1)
    ll.add_last(1)
    ll.add_last(2)
    ll.add_last(2)
    ll.add_last(2)
    ll.add_last(2)
    ll.add_last(3)
    ll.add_last(4)
    ll.add_last(5)
    ll.add_last(5)
    ll.add_last(5)
    ll.add_last(5)
    ll.print_list()
    print()