def test_search(self): sll = Sll() # Test an empty Linked List assert sll.search(5) == 'Linked list is empty' # Test Linked List with some nodes sll.add_at_head(1) sll.add_at_head(2) sll.add_at_head(3) assert sll.search('python') is False assert sll.search(3) is True
def test_reverse_iter(self): sll = Sll() sll.add_at_head(1) sll.add_at_head(2) sll.add_at_head(3) result = str(sll.head) assert result == str(3) sll.reverse_iter() assert str(sll.head) == str(1)
def test_list_size(self): sll = Sll() size = sll.list_size() # Test an empty Linked List assert size == 0 # Test Linked List with some nodes sll.add_at_head(1) sll.add_at_head(2) sll.add_at_head(3) sll.add_at_head(4) size = sll.list_size() assert size == 4
def sum_two_llist(head1, head2): curr_1 = head1 curr_2 = head2 # Create a new linked list new_llist = Sll() carry = 0 while curr_1 and curr_2: if not curr_1: i = 0 else: i = curr_1.data if not curr_2: j = 0 else: j = curr_2.data sum_of_lists = i + j + carry if sum_of_lists >= 10: carry = 1 remainder = sum_of_lists % 10 new_llist.add_at_tail(remainder) else: carry = 0 new_llist.add_at_tail(sum_of_lists) if curr_1: curr_1 = curr_1.next if curr_2: curr_2 = curr_2.next if carry > 0: new_llist.add_at_tail(carry) new_llist.print_list()
def test_remove(self): sll = Sll() # Test an empty Linked List assert sll.remove(5) == "Linked list is empty" # Test Linked List with some nodes sll.add_at_head(1) sll.add_at_head(2) sll.add_at_head(3) assert str(sll.remove(24)) == 'A Node with given data is not present.' assert sll.search(2) is True sll.remove(2) assert sll.search(2) is False
def test_add_at_head(self): sll = Sll() sll.add_at_head('cpp') result = str(sll.head) expected = 'cpp' assert result == expected
def test_is_empty(self): sll = Sll() assert sll.is_empty() is True node = SllNode(1) sll.head = node assert sll.is_empty() is False
while current: if current.data in duplicates: # Remove previous.next = current.next current = None else: # If first occurrence, store data in a dictionary duplicates[current.data] = 1 previous = current current = previous.next if __name__ == "__main__": sll = Sll() sll.add_at_tail(1) sll.add_at_tail(2) sll.add_at_tail(3) sll.add_at_tail(2) sll.add_at_tail(4) sll.add_at_tail(2) sll.add_at_tail(1) sll.add_at_tail(4) print("List: ") sll.print_list() print("\n") print("List after removing duplicates: ") remove_duplicates(sll.head) sll.print_list()
new_llist.add_at_tail(sum_of_lists) if curr_1: curr_1 = curr_1.next if curr_2: curr_2 = curr_2.next if carry > 0: new_llist.add_at_tail(carry) new_llist.print_list() if __name__ == "__main__": sll1 = Sll() sll1.add_at_tail(4) sll1.add_at_tail(7) sll1.add_at_tail(3) sll2 = Sll() sll2.add_at_tail(7) sll2.add_at_tail(8) sll2.add_at_tail(7) print("List1: ") sll1.print_list() print("List2: ") sll2.print_list() print("\n") print("Sum of two lists: ") sum_two_llist(sll1.head, sll2.head)
curr_1 = temp.next else: temp.next = curr_2 temp = curr_2 curr_2 = temp.next if not curr_1: temp.next = curr_2 if not curr_2: temp.next = curr_1 return new_head if __name__ == "__main__": sll1 = Sll() sll1.add_at_tail(3) sll1.add_at_tail(4) sll1.add_at_tail(6) sll1.add_at_tail(8) sll2 = Sll() sll2.add_at_tail(1) sll2.add_at_tail(2) sll2.add_at_tail(5) sll2.add_at_tail(7) print("List1: ") sll1.print_list() print("List2: ") sll2.print_list() print("\n")
from singlylinkedlist.singly_linkedlist import Sll def reverse_linked_list(current): """Reverse a linked list using recursion.""" if current.next is None: sll.head = current return sll.head reverse_linked_list(current.next) temp = current.next temp.next = current current.next = None if __name__ == "__main__": sll = Sll() sll.add_at_head(1) sll.add_at_head(3) sll.add_at_head(5) sll.add_at_head(7) print("Elements of linked list:") sll.print_list() print("Head:") print(sll.head) print("Elements of reversed linked list:") reverse_linked_list(sll.head) sll.print_list() print("Head after reversing:") print(sll.head)
def print_forward_list(current): """Print all the elements of the linked list using recursion.""" if current is None: return "Exit the linked list." print(current.data) print_forward_list(current.next) def print_reverse_list(current): """Print all the elements of the linked list after reversing using recursion.""" if current is None: return "Exit the linked list." print_reverse_list(current.next) print(current.data) if __name__ == "__main__": sll = Sll() sll.add_at_head(1) sll.add_at_head(3) sll.add_at_head(5) sll.add_at_head(7) print("Elements of linked list:") print(sll.head) print("Head of linked list:") print_forward_list(sll.head) print("Elements of reversed linked list:") print_reverse_list(sll.head)
# # i = 0 # while curr_2: # prev.append(curr_2) # curr_2 = curr_2.next # i += 1 # curr_2 = prev[i-1] # # count = 1 # while count <= i//2 + 1: # if prev[-count].data != curr_1.data: # return False # curr_1 = curr_1.next # count += 1 # return True if __name__ == "__main__": sll = Sll() sll.add_at_tail("K") sll.add_at_tail("A") sll.add_at_tail("Y") sll.add_at_tail("A") sll.add_at_tail("K") print("List: ") sll.print_list() print("\n") print("Is Palindrome? " + str(is_palindrome(sll.head)))
current = head while current: if current in s: return True # If first occurrence, store data in hash s.add(current) current = current.next return False if __name__ == "__main__": sll = Sll() sll.add_at_tail(1) sll.add_at_tail(2) sll.add_at_tail(3) sll.add_at_tail(4) # Create a loop for testing temp1 = sll.head.next temp2 = temp1.next.next temp2.next = temp1 # If loop found print True, else print False print(detect_loop_llist(sll.head))
from circularlinkedlist.circular_singlylinkedlist import CSll from singlylinkedlist.singly_linkedlist import Sll def is_circular_linkedlist(input_list): current = input_list.head while current.next: current = current.next if current.next == input_list.head: return True return False if __name__ == "__main__": s = Sll() s.add_at_head(1) s.add_at_head(2) s.add_at_head(3) s.add_at_head(4) cll = CSll() cll.add_at_first(1) cll.add_at_first(2) cll.add_at_first(3) cll.add_at_first(4) print(is_circular_linkedlist(s)) print(is_circular_linkedlist(cll))
if not key_1 or not key_2: return # If both keys are present if prev_1: # If not head node prev_1.next = curr_2 else: # If head node sll.head = curr_2 if prev_2: # If not head node prev_2.next = curr_1 else: # If head node sll.head = curr_1 curr_1.next, curr_2.next = curr_2.next, curr_1.next if __name__ == "__main__": sll = Sll() sll.add_at_tail("A") sll.add_at_tail("B") sll.add_at_tail("C") sll.add_at_tail("D") print("List: ") sll.print_list() print("\n") print("List after swaping: ") swap_nodes(sll, "A", "C") sll.print_list()