diff --git a/data_structures/linked_list/kth_element_towards_head.py b/data_structures/linked_list/kth_element_towards_head.py new file mode 100644 index 000000000000..b7c534a10164 --- /dev/null +++ b/data_structures/linked_list/kth_element_towards_head.py @@ -0,0 +1,36 @@ +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class LinkedList: + def __init__(self): + self.head = None + + def append(self, data): + if not self.head: + self.head = Node(data) + else: + current = self.head + while current.next: + current = current.next + current.next = Node(data) + + def find_kth_node(self, k): + current = self.head + count = 0 + while current: + count += 1 + if count == k: + return current.data + current = current.next + return None + +linked_list = LinkedList() +linked_list.append(1) +linked_list.append(2) +linked_list.append(3) +linked_list.append(4) +linked_list.append(5) + +print(linked_list.find_kth_node(3)) \ No newline at end of file