Create remove_kth_node.py

This commit is contained in:
marina 2023-08-02 17:46:34 -07:00 committed by GitHub
parent eb36c2249d
commit 31c7cc4654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,31 @@
#!/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
node = head
lenght = 0
# find the lenght of the list
while node:
node = node.next
lenght += 1
if n == lenght:
return head.next
i = 0
node = head
while i < lenght - n - 1:
node = node.next
i += 1
node.next = node.next.next
return head