-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
36 lines (34 loc) · 876 Bytes
/
main.py
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(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
dummy = ListNode(-1)
dummy.next = head
fast, slow = head, dummy
for _ in range(n):
fast = fast.next
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next
import os
if os.getenv('LZS'):
dummy = ListNode(-1)
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)
dummy.next = head
s = Solution()
head = s.removeNthFromEnd(head, 3)
while head:
print(head.val)
head = head.next