Example #1
0
    # Traversing/Searching for Node to Delete
    while current_node is not None:
        # Node to delete is found
        if value is current_node.data:
            # previous node now points to next node
            previous_node.next_element = current_node.next_element
            current_node.next_element = None
            deleted = True
            break
        previous_node = current_node
        current_node = current_node.next_element

    if deleted is False:
        print(str(value) + " is not in list!")
    else:
        print(str(value) + " deleted!")

    return deleted


lst = LinkedList()
lst.insert_at_head(1)
lst.insert_at_head(4)
lst.insert_at_head(3)
lst.insert_at_head(2)
lst.print_list()
delete(lst, 4)
lst.print_list()

# delete(lst, 2)  # Python is stranger
Example #2
0
# Node class  { int data ; Node next_element;}


def reverse(lst):
    # To reverse linked, we need to keep track of three things
    previous_node = None  # Maintain track of the previous node
    current = lst.get_head()  # The current node
    next_node = None  # The next node in the list

    # Reversal
    while current:
        next_node = current.next_element
        current.next_element = previous_node
        previous_node = current
        current = next_node

    # Set the last element as the new head node
    lst.head_node = previous_node
    return lst


lst = LinkedList()
lst.insert_at_head(6)
lst.insert_at_head(4)
lst.insert_at_head(9)
lst.insert_at_head(10)
lst.print_list()

reverse(lst)
lst.print_list()
Example #3
0
from DataStructures.IntroLinkedLists.LinkedList import LinkedList


def length(lst):
    # start from the first element
    curr = lst.get_head()
    length = 0

    # Traverse the list and count the number of nodes
    while curr:
        length += 1
        curr = curr.next_element
    return length


lst = LinkedList()
lst.insert_at_head(4)
lst.insert_at_head(3)
lst.insert_at_head(2)
lst.insert_at_head(1)
lst.insert_at_head(0)
print(length(lst))
Example #4
0
    # Traverse the list and put each node in the visitedNodes list
    # and if a node appears twice in the map
    # then it means there is a loop in the list
    while current_node:
        if current_node in visited_nodes:
            return True
        visited_nodes.append(current_node)  # Insert node in visitedNodes list
        current_node = current_node.next_element
    return False


# ------------------------------

lst = LinkedList()

lst.insert_at_head(21)
lst.insert_at_head(14)
lst.insert_at_head(7)

head = lst.get_head()
node = lst.get_head()

# Adding a loop
for i in range(4):
    if node.next_element is None:
        node.next_element = head.next_element
        break
    node = node.next_element

print(detect_loop(lst))
Example #5
0
from DataStructures.IntroLinkedLists.LinkedList import LinkedList


def delete_at_head(lst):
    # Get Head and firstElement of List
    first_element = lst.get_head()

    # if List is not empty then link head to the
    # nextElement of firstElement.
    if first_element is not None:
        lst.head_node = first_element.next_element
        first_element.next_element = None
    return


lst = LinkedList()
for i in range(11):
    lst.insert_at_head(i)

lst.print_list()

delete_at_head(lst)
delete_at_head(lst)

lst.print_list()
            return True  # value found
        current_node = current_node.next_element

    return False  # value not found


# Solution: Recursive
def searchRec(current_node, value):
    if current_node is None:
        return False
    if current_node.data is value:
        return True
    return searchRec(current_node.next_element, value)


def searchSol2(lst, value):
    # Start from first element
    current_node = lst.get_head()
    return searchRec(current_node, value)


list = LinkedList()
list.insert_at_head(4)
list.insert_at_head(10)
list.insert_at_head(90)
list.insert_at_head(5)
list.print_list()
print(search(list, 4))
print(searchSol1(list, 4))
print(searchSol2(list, 4))