def ispalindrome(linked_list): if not linked_list.head: return False aux = linked_list.head size = 0 while aux: size += 1 aux = aux.next if size == 1: # maybe removed return True i = 1 ll = None while i <= round(size / 2): if not ll: ll = LinkedList(linked_list.head.value) else: new_head = Node(linked_list.head.value) new_head.next = ll.head ll.head = new_head if size % 2 == 0 or i != round(size / 2): linked_list.head = linked_list.head.next i += 1 return equals(ll, linked_list)
aux = aux.next size += 1 if kth >= size: return complement = size - kth aux = linked_list.head i = 1 while i != complement: aux = aux.next i += 1 return aux.value ll = LinkedList(5) ll.add(4) ll.add(3) ll.add(2) ll.add(1) ll_one_elem = LinkedList(1) print(return_kth_to_last(ll, 1)) print(return_kth_to_last(ll, 2)) print(return_kth_to_last(ll, 3)) print(return_kth_to_last(ll, 4)) print(return_kth_to_last(ll, 5)) print(return_kth_to_last(ll, 6)) print(return_kth_to_last(ll_one_elem, 0))
from linked_list.likedlist import LinkedList # Remove duplications from a linked list # Space O(1). Time O(n²) def remove_dup(linked_list): first = linked_list.head while first and first.next: second = first while second.next: if first.value == second.next.value: second.next = second.next.next else: second = second.next first = first.next ll = LinkedList(5) ll.add(4) ll.add(3) ll.add(5) ll.add(4) ll.add(3) print(ll) remove_dup(ll) print(ll)
if first_size > second_size: while first_size != second_size: aux1 = aux1.next first_size -= 1 elif second_size > first_size: while second_size != first_size: aux2 = aux2.next second_size -= 1 while aux1 and aux2: if aux1 == aux2: return aux1 aux1 = aux1.next aux2 = aux2.next return first_ll = LinkedList('1') first_ll.add('2') first_ll.add('3') first_ll.add('4') first_ll.add('5') second_ll = copy.copy(first_ll) second_ll.head = second_ll.head.next new_head = Node(9) new_head.next = second_ll.head.next second_ll.head = new_head print(intersection(first_ll, second_ll))
val2 = aux2.value if aux2 else 0 if not aux1: aux1_prev.next = Node((val1 + val2 + carry) % 10) else: aux1.value = (val1 + val2 + carry) % 10 carry = 1 if val1 + val2 + carry >= 10 else 0 aux1_prev = aux1 if aux1 else aux1_prev.next if aux1: aux1 = aux1.next if aux2: aux2 = aux2.next return list_one first_ll = LinkedList(1) first_ll.add(3) first_ll.add(5) second_ll = LinkedList(2) second_ll.add(4) second_ll.add(1) # first_ll = LinkedList(9) # first_ll.add(5) # first_ll.add(1) # # second_ll = LinkedList(7) # second_ll.add(4) # second_ll.add(1)
ll = None while i <= round(size / 2): if not ll: ll = LinkedList(linked_list.head.value) else: new_head = Node(linked_list.head.value) new_head.next = ll.head ll.head = new_head if size % 2 == 0 or i != round(size / 2): linked_list.head = linked_list.head.next i += 1 return equals(ll, linked_list) ll = LinkedList('a') print(ispalindrome(ll)) ll = LinkedList('a') ll.add('a') print(ispalindrome(ll)) ll = LinkedList('t') ll.add('a') ll.add('c') ll.add('c') ll.add('a') ll.add('t') print(ispalindrome(ll)) ll = LinkedList('t')
# Check is a linked lists contains a loop. Return the node at beginning of loop. # Space O(n). Time O(n) def loop_detect(linked_list): if not linked_list.head: return dict = {} aux = linked_list.head while aux: if dict.get(aux): return aux dict[aux] = 1 aux = aux.next circular_ll = LinkedList('A') circular_ll.add('B') circular_ll.add('C') circular_ll.add('D') circular_ll.add('E') tail = circular_ll.head.next.next.next.next node_c = circular_ll.head.next.next tail.next = node_c print(loop_detect(circular_ll)) not_circular_ll = LinkedList('A') not_circular_ll.add('B') not_circular_ll.add('C') not_circular_ll.add('D') not_circular_ll.add('E') print(loop_detect(not_circular_ll))