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 eaee7b3 commit 82ed731Copy full SHA for 82ed731
1 file changed
remove-nth-node-from-end-of-list/8804who.py
@@ -0,0 +1,32 @@
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
+
11
+ temp = head
12
+ length = 0
13
14
+ while temp:
15
+ length += 1
16
+ temp = temp.next
17
18
+ return self.removeNth(head, length-n+1)
19
20
+ def removeNth(self, head, n):
21
+ if n == 1:
22
+ if not head.next:
23
24
+ else:
25
+ return head.next
26
27
28
+ return head
29
+ head.next = self.removeNth(head.next, n-1)
30
31
32
0 commit comments