-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove_nth_node_from_end_in_LL.py
More file actions
36 lines (35 loc) · 1 KB
/
Remove_nth_node_from_end_in_LL.py
File metadata and controls
36 lines (35 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
"""temp=head # sol 1
n_temp=head
l=0
while(temp):
l=l+1
temp=temp.next
index_to_remove=l-n
if (index_to_remove==0):
head=head.next
return head
else:
count=0
while(n_temp):
if count==index_to_remove-1:
n_temp.next=n_temp.next.next
break
n_temp=n_temp.next
count=count+1
return head"""
temp=ListNode(0,head) # soln 2
slow=fast=temp
for i in range(n):
fast=fast.next
while(fast.next):
slow=slow.next
fast=fast.next
slow.next=slow.next.next
return temp.next