def main(): print("Find Kth element from last") _list = linkedlist.create_list(10, 10) linkedlist.print_list(_list) for i in range(13): res = FindKthElement(_list, i) if res: value = res.value else: value = -1 print("\t%d from last: %d" % (i, value))
import linkedlist from linkedlist import LinkedList def delete_el(node): if node and node.next: node.data, node.next.data = node.next.data, node.data node.next = node.next.next linkedlist.printlist(head) else: print("Can't delete") list1 = LinkedList() head = linkedlist.create_list(list1) element = input("Enter element to be deleted: ") if head is None: print("List is empty") else: start = head while start: if start.data == element: break start = start.next if start is None: print("Element not in the list") else: delete_el(start)
def main(): head = linkedlist.create_list(20, 10) linkedlist.print_list(head) new_list = rem_dups(head) linkedlist.print_list(new_list)
def main(): _list = linkedlist.create_list(5, 10) linkedlist.print_list(_list) _list = del_mid(_list) linkedlist.print_list(_list)
def add_lists(head1, head2, carry=0): if head1 == None and head2 == None and carry == 0: return None result_list = LinkedList() value = carry if head1 is not None: value += head1.data if head2 is not None: value += head2.data result_list.data = value % 10 if head1 is not None or head2 is not None: result_list.next = add_lists(None if head1 == None else head1.next, None if head2 == None else head2.next, 1 if value >= 10 else 0) return result_list list1 = LinkedList() head1 = linkedlist.create_list(list1) list2 = LinkedList() head2 = linkedlist.create_list(list2) if head1 is None or head2 is None: print("List is empty") else: result_head = add_lists(head1, head2) linkedlist.printlist(result_head)