Skip to content

Commit

Permalink
really a strange problem
Browse files Browse the repository at this point in the history
  • Loading branch information
viewv committed Oct 5, 2018
1 parent ffdf576 commit 00b533a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 19. Remove Nth Node From End of List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None


class Solution:
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
stack = []
p = head
while p != None:
stack.append(p)
p = p.next
if len(stack) == 1:
return None
if len(stack) == n:
head = stack[1]
return head
if n == 1:
stack[-2].next = None
else:
stack[-1 * n - 1].next = stack[-1 * n + 1]
return head

0 comments on commit 00b533a

Please sign in to comment.