leetcode_Remove Nth Node From End of List

Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head.
(删除链表尾开始的第 N 个)

Example:



1. 正向定位

这个是一个简单的链表增删的问题。从后往前删除,我们可以先通过遍历一次确定链表的长度,从而可以正向的定位到需要删除的位置。

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
# 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
"""
p = head
length = 0
while p != None:
p = p.next
length += 1

if n > length:
return head
elif n == length:
return head.next
else:
p = head
k = length - n - 1
while k != 0:
k -= 1
p = p.next
p.next = p.next.next
return head