# Question: Implement an algorithm to find the kth to last element of a singly linked list. from linked_list import Node def delete_middle_node(n): # Description: Update data of current node with data of next node and delete the next node. Assumption here is that input node is neither the first nor the last node of the singly linked list. # Time: O(1) # Space: O(1) n.data = n.next.data n.next = n.next.next if __name__ == '__main__': head = Node(1) head.append_to_tail(5) head.append_to_tail(9) head.append_to_tail(12) head.print() n = head while n.data != 9: n = n.next print(n.data) delete_middle_node(n) head.print()
# Description: Hint #126 - Can you do it iteratively? Imagine if you had two pointers pointing to adjacent nodes and they were moving at the same speed through the linked list. When one hits the end of the linked list, where will the other be? # Time: O(N) # Space: O(1) p1 = head p2 = head for i in range(k): if not p2.next: return p2 = p2.next while p2.next: p1 = p1.next p2 = p2.next return p1 if __name__ == '__main__': n = None for i in range(10): if not n: n = Node(i) else: n.append_to_tail(i) n.print() k = 3 print(get_kth_to_last_element(n, k).data)