Skip to content

Commit 882c39b

Browse files
committed
Time: 39 ms (41.13%) | Memory: 16.5 MB (66.24%) - LeetSync
1 parent 7ebfbf3 commit 882c39b

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
8+
if not head:
9+
return None
10+
if not head.next:
11+
return None
12+
13+
14+
fast = head
15+
16+
for i in range(n):
17+
fast= fast.next
18+
if not fast:
19+
return head.next
20+
21+
slow = head
22+
if fast:
23+
while fast.next != None:
24+
slow = slow.next
25+
fast = fast.next
26+
27+
slow.next = slow.next.next
28+
29+
30+
# prev.next = temp.next
31+
return head
32+
33+

0 commit comments

Comments
 (0)