problem description:
remove the nth node from the end of the list
for example:
given: 1->2->3 n = 1
return: 1->2
thought:
first:you should know the length
second:if the del node is the first node ,just return head.next;esle find the prior node of the del node
third: use a node record the prior node,and make the node.next = del.node.next
last:return head
there is my python solution:
# 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
"""
i = 0
first = head
while first:
i += 1
first = first.next
first=head
if i-n==0:
return head.next
j = 0
while j<i-n-1:
first = first.next
j += 1
second = first
first = first.next
second.next = first.next
return head
it can beat 78% python users