-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRemoveNthNodeFromEndOfList.py
More file actions
43 lines (40 loc) · 1.13 KB
/
Copy pathRemoveNthNodeFromEndOfList.py
File metadata and controls
43 lines (40 loc) · 1.13 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
37
38
39
40
41
42
43
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
tail = head
length = 1
while tail.next:
length += 1
tail = tail.next
if(length==1):
return None
if(length==n):
return head.next
tempnode = head
for _ in range(0, length-n-1):
tempnode = tempnode.next
tempnode.next = tempnode.next.next
return head
# One Pass
# 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:
dummy = fast = slow = ListNode()
dummy.next = head
if not head.next:
return None
for _ in range(n+1):
fast = fast.next
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next