Skip to content

Commit 6e99c79

Browse files
committed
Remove Kth Node From End of Linked List
1 parent 425ad28 commit 6e99c79

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Write a function that takes in the head of a Singly Linked List and
2+
# an integer k (assume that the list has at least k nodes).
3+
# The function should remove the kth node from the end of the list.
4+
# Note that every node in the Singly Linked List has a "value" property storing its value as well as a "next" property
5+
# pointing to the next node in the list or None (null) if it is the tail of the list.
6+
7+
8+
def removeKthNodeFromEnd(head, k):
9+
count = 1
10+
first = head
11+
second = head
12+
while count <= k:
13+
count += 1
14+
second = second.next
15+
if not second:
16+
head.value = head.next.value
17+
head.next = head.next.next
18+
return
19+
while second.next:
20+
first = first.next
21+
second = second.next
22+
first.next = first.next.next

0 commit comments

Comments
 (0)