# 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
# Access head_node => list.get_head() # Check if list is empty => list.is_empty() # Node class {int data ; Node next_element;} # Inserts a value at the end of the list from DataStructures.IntroLinkedLists.LinkedList import LinkedList from DataStructures.IntroLinkedLists.Node import Node # HVN # def insert_at_tail(lst, value): # lst.insert_at_tail(value) list = LinkedList() list.print_list() # print("Inserting values in first list") # for i in range(1, 10): # list.insert_at_head(i) print("Inserting values in last list") for i in range(1, 10): list.insert_at_tail(i) list.print_list() # Sol def insert_at_tail(lst, value): # Creating a new node new_node = Node(value)
# 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()
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))
current_node = lst.get_head() # 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
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))