We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0b9f213 commit 0d22f4bCopy full SHA for 0d22f4b
Leetcode/remove_nth_node.py
@@ -0,0 +1,23 @@
1
+# 19. Remove Nth Node From End of List
2
+
3
+# Definition for singly-linked list.
4
+# class ListNode:
5
+# def __init__(self, val=0, next=None):
6
+# self.val = val
7
+# self.next = next
8
+class Solution:
9
+ def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
10
+ prev_end_node = dummy_head = ListNode(next=head)
11
+ cur_node = head
12
+ i = 1
13
+ while i < n:
14
+ cur_node = cur_node.next
15
+ i += 1
16
17
+ while cur_node.next:
18
19
+ prev_end_node = prev_end_node.next
20
21
+ prev_end_node.next = prev_end_node.next.next
22
23
+ return dummy_head.next
0 commit comments