master-algorithms-py/linked_lists/remove_kth_node.py
2023-08-07 16:17:11 -07:00

31 lines
664 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def remove_kth_node(self, head, n):
if head is None or head.next is None:
return None
# find the length of the list
node, length = head, 0
while node:
node = node.next
length += 1
# if n is the entire list, remove head
if n == length:
return head.next
# loop to kth element
node, i = head, 0
while i <= length - n:
node = node.next
i += 1
# remove the kth element
node.next = node.next.next
return head