mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create remove_kth_node.py
This commit is contained in:
parent
eb36c2249d
commit
31c7cc4654
31
linked_lists/remove_kth_node.py
Normal file
31
linked_lists/remove_kth_node.py
Normal 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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user