Skip to content

Commit 00b533a

Browse files
committed
really a strange problem
1 parent ffdf576 commit 00b533a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
8+
class Solution:
9+
def removeNthFromEnd(self, head, n):
10+
"""
11+
:type head: ListNode
12+
:type n: int
13+
:rtype: ListNode
14+
"""
15+
stack = []
16+
p = head
17+
while p != None:
18+
stack.append(p)
19+
p = p.next
20+
if len(stack) == 1:
21+
return None
22+
if len(stack) == n:
23+
head = stack[1]
24+
return head
25+
if n == 1:
26+
stack[-2].next = None
27+
else:
28+
stack[-1 * n - 1].next = stack[-1 * n + 1]
29+
return head

0 commit comments

Comments
 (0)