Пример #1
0
        return False
    if (list.length() <= 1):
        return True
    temp = list.head
    re = ''
    while (temp):
        re = re + temp.val
        temp = temp.next
    return re == re[::-1]


if __name__ == '__main__':

    # Start with the empty list
    llist = LinkedList()

    # Insert a.  So linked list becomes 6->None
    llist.append('a')

    # Insert b at the beginning. So linked list becomes b->a->None
    llist.push('b')

    # Insert b at the beginning. So linked list becomes b->a->b->None
    llist.append('b')

    # print list
    llist.printList()

    print(checkPalindro(llist))
    print('-------------')
    print(checkPalindro_2(llist))
Пример #2
0
#  main function 
if __name__ == '__main__':
    #  sample data 
    llist = LinkedList()
    llist.head = Node(1)
    second = Node(2)
    third = Node(3)

    llist.head.next = second;
    second.next = third;
    # intial Linked List
    llist.printList()

    #  Inserting node at the front 
    llist.push(0)

    # updating the Linked List
    print("new updated list after adding element at the stating of the list")
    llist.printList()

    # Inserting element after specific element
    llist.insertAfterData(2, 4)
    print("updated linked list after insertion after the given node")
    llist.printList()

    llist.insertAfterNode(second, 5)
    print("updated linked list after insertion after the given node")
    llist.printList()

    llist.append(6)
Пример #3
0
    while (temp.next):
        if (temp.next.val in vals):
            new = temp.next.next
            temp.next = new
        else:
            vals.add(temp.next.val)
            temp = temp.next
    return list


if __name__ == '__main__':
    llist = LinkedList()

    # test remove duplicates in a sorted list

    llist.push(20)
    llist.push(13)
    llist.push(13)
    llist.push(11)
    llist.push(11)
    llist.push(11)
    print("Created Linked List: ")
    llist.printList()
    print()
    print("Linked List after removing", "duplicate elements:")
    llist = removeDup(llist)
    llist.printList()

    # test remove duplicates in an unsorted list
    list = LinkedList()
    list.append(10)
Пример #4
0
        fast = fast_next.next
        slow = slow.next

    return slow.val


if __name__ == '__main__':

    # Start with the empty list
    llist = LinkedList()

    # Insert 6.  So linked list becomes 6->None
    llist.append(6)

    # Insert 7 at the beginning. So linked list becomes 7->6->None
    llist.push(7)

    # Insert 1 at the beginning. So linked list becomes 1->7->6->None
    llist.push(1)

    # Insert 4 at the end. So linked list becomes 1->7->6->4->None
    llist.append(4)

    # Insert 10 at the end. So linked list becomes 1->7->6->4->10->None
    llist.append(10)

    # Insert 8, after 7. So linked list becomes 1 -> 7-> 8-> 6-> 4-> 10 ->None
    llist.insertAfter(llist.head.next, 8)

    # print list
    llist.printList()