原题
Given a linked list, remove the n-th node from the end of list and return its head.
题目:给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
给定一个链表: 1->2->3->4->5, 和 n = 2.
当删除了倒数第二个节点后,链表变为 1->2->3->5.
Note:
Given
will always be valid.
给定的 n 保证是有效的。
My Solution
# 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
"""
if n == 0 or head is None:
return head
len_list = 1
link_index = head
link_sel_index = head
while True:
if link_index.next is None:
if len_list == n:
return head.next
if len_list > 1:
link_sel_index.next = link_sel_index.next.next
break
if len_list == 1 and n > 1:
return False
else:
if len_list > n:
link_sel_index = link_sel_index.next
link_index = link_index.next
len_list += 1
return head
Reference solution
分析:之前我们说过 C 和 C++ 中的指针是个好东西 ,在解决这类问题很是方便 。然而 python 是没有这个概念的 ,包括链表也是模拟链表的相关操作 。刷题到这 ,小詹也得考虑总结下 python 中链表的相关操作了 。
思路一 :首先利用一次循环得到链表长度 ,之后根据题目输入的参数在指定位置进行删除操作 ,即要删除第 L-n+1 个节点 ,只需要将第 L-n 个节点的指针指向第 L-n+2 个节点即可跳过要删除的节点 。(在第 L-n+2 个节点存在的情况下 ,不存在即要删除倒数第一个节点 ,是个特例情况 。)
思路二:这里考虑用两个 ‘指针’ 进行操作 ,要删除倒数第 n 个节点 ,即与最后一个节点的间隔是固定的 (n-1)。这里先将两个指针同时指向头结点 ,然后第一个指针后移对应位置 ,使得两个指针间隔为 n-1 。之后两个指针同步后移 ,当先移动的指针指到最后的时候 ,第二个指针进行跳过删除操作 ,即可实现目的 。
class Solution:
#对应上边小詹说的第二种思路
def removeNthFromEnd(self, head, n):
first = second = head
#将第一个‘指针’移动 n 个位置
for _ in range(n):
first = first.next
#当极端情况,即first指向了最后一个节点,且要删的是第一个节点(倒数第n个)
if not first:
return head.next
#将两个‘指针’同步后移
#直到first指向了最后一个节点(两个‘指针’始终保持相同间隔)
while first.next:
first = first.next
second = second.next
second.next = second.next.next
return head
反思:
- python里的链表是伪链表,是用list模拟的操纵;
- 不能直接使用len(head)来获取链表长度,因为head只是链表的第一个元素或者指针;