Esempio n. 1
0
def main():
    head = Node(10)
    head.appendToTail(20, 's')
    head.appendToTail(46, 's')
    head.appendToTail(7654, 's')
    head.printLinkedList()
    print(kthElement(head, 3))
    head.printLinkedList()
Esempio n. 2
0
def main():
    head = Node(10)
    head.appendToTail(20, 's')
    head.appendToTail(46, 's')
    head.appendToTail(7654, 's')
    head.printLinkedList()
    midNode = head.findNode(10)
    delMiddleNode(midNode)
    head.printLinkedList()
Esempio n. 3
0
def main():
    head = Node(10)
    head.appendToTail(20)
    head.appendToTail(50)  # Dupe
    head.appendToTail(30)
    head.appendToTail(120)
    head.appendToTail(50)  # Dupe
    head.appendToTail(10)
    head.appendToTail(15)
    head.printLinkedList()
    removeDupesDoubly(head)  # Should remove Dupes from LinkedList
    head.printLinkedList()
    return
Esempio n. 4
0
    while(p):
        if p.val < pValue:
            before.append(p.val)
        elif p.val == pValue:
            p = p.next
            continue
        else:
            after.append(p.val)
        p = p.next
    # Make new list with before - pValue - after
    new_list = []
    new_list.extend(before)
    new_list.append(pValue)
    new_list.extend(after)
    p = head
    # iterate through linkedlist and change values
    for i in range(len(new_list)):
        p.val = new_list[i]
        p = p.next
    return


if __name__ in '__main__':
    head = Node(10)
    head.appendToTail(46, 's')
    head.appendToTail(20, 's')
    head.appendToTail(7654, 's')
    head.printLinkedList()
    partition_linked_list(head, 46)
    head.printLinkedList()
Esempio n. 5
0
    while (p):
        if p.val < pValue:
            before.append(p.val)
        elif p.val == pValue:
            p = p.next
            continue
        else:
            after.append(p.val)
        p = p.next
    # Make new list with before - pValue - after
    new_list = []
    new_list.extend(before)
    new_list.append(pValue)
    new_list.extend(after)
    p = head
    # iterate through linkedlist and change values
    for i in range(len(new_list)):
        p.val = new_list[i]
        p = p.next
    return


if __name__ in '__main__':
    head = Node(10)
    head.appendToTail(46, 's')
    head.appendToTail(20, 's')
    head.appendToTail(7654, 's')
    head.printLinkedList()
    partition_linked_list(head, 46)
    head.printLinkedList()
Esempio n. 6
0
from Data_structures import Node


def del_middle_node(mid):
    """
        Will Delete Middle Node in singly linkedList,
        will return error if last node referenced,
        will DELETE the head node, if given head
    """
    if mid is None:
        raise ValueError("None Value given")
    if mid.next is None:
        raise ValueError("last node given")
    mid.val = mid.next.val
    # If 2nd last node
    if mid.next.next is None:
        mid.next = None
    else:
        mid.next = mid.next.next


if __name__ in '__main__':
    head = Node(10)
    head.appendToTail(20, 's')
    head.appendToTail(46, 's')
    head.appendToTail(7654, 's')
    head.printLinkedList()
    midNode = head.findNode(10)
    del_middle_node(midNode)
    head.printLinkedList()
Esempio n. 7
0
    pointer = pointer.next
    while(pointer):
        # Seen this value before
        if pointer.val in my_set:
            # If End of LinkedList
            if pointer.next is None:
                pointer_prev.next = None
                break
            else:
                pointer_prev.next = pointer.next
        # Not seen before
        else:
            my_set.add(pointer.val)
            pointer_prev = pointer
        pointer = pointer.next
    return head


if __name__ == '__main__':
    head = Node(10)
    head.appendToTail(20)
    head.appendToTail(50)  # Dupe
    head.appendToTail(30)
    head.appendToTail(120)
    head.appendToTail(50)  # Dupe
    head.appendToTail(10)
    head.appendToTail(15)
    head.printLinkedList()
    remove_dupes_singly(head)  # Should remove Dupes from LinkedList
    head.printLinkedList()
Esempio n. 8
0
    pointer = pointer.next
    while (pointer):
        # Seen this value before
        if pointer.val in my_set:
            # If End of LinkedList
            if pointer.next is None:
                pointer_prev.next = None
                break
            else:
                pointer_prev.next = pointer.next
        # Not seen before
        else:
            my_set.add(pointer.val)
            pointer_prev = pointer
        pointer = pointer.next
    return head


if __name__ == '__main__':
    head = Node(10)
    head.appendToTail(20)
    head.appendToTail(50)  # Dupe
    head.appendToTail(30)
    head.appendToTail(120)
    head.appendToTail(50)  # Dupe
    head.appendToTail(10)
    head.appendToTail(15)
    head.printLinkedList()
    remove_dupes_singly(head)  # Should remove Dupes from LinkedList
    head.printLinkedList()
Esempio n. 9
0
            return False
    return True


def is_palindrome_stack(head):
    myStack = Stack()
    p = head
    # First Pass, put all values in Stack
    while (p is not None):
        myStack.push(p.val)
        p = p.next
    # second pass
    p = head
    while (p is not None):
        if p.val != myStack.pop():
            return False
        else:
            p = p.next
    return True


if __name__ in '__main__':
    head = Node('R')
    head.appendToTail('A')
    head.appendToTail('D')
    head.appendToTail('A')
    head.appendToTail('R')
    head.printLinkedList()
    print(is_palindrome_node(head))
    print(is_palindrome_stack(head))