From 0389271f6b52a72a2729660bebf1518adf32b057 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:17:11 -0700 Subject: [PATCH] Update remove_kth_node.py --- linked_lists/remove_kth_node.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/linked_lists/remove_kth_node.py b/linked_lists/remove_kth_node.py index 9898783..db7e14e 100644 --- a/linked_lists/remove_kth_node.py +++ b/linked_lists/remove_kth_node.py @@ -7,25 +7,24 @@ def remove_kth_node(self, head, n): if head is None or head.next is None: return None - - node = head - lenght = 0 - - # find the lenght of the list + + # find the length of the list + node, length = head, 0 while node: node = node.next - lenght += 1 - - if n == lenght: + length += 1 + + # if n is the entire list, remove head + if n == length: return head.next - i = 0 - node = head - while i < lenght - n - 1: + # 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 -