Leetcode19 删除链表指定节点

思路:用列表保存链表,然后分情况讨论。

python 复制代码
class Solution:
    def removeNthFromEnd(self, head, n: int):
        node_list=[head]
        while head.next:
            head=head.next
            node_list.append(head)
        remove_loc=len(node_list)-n
        #要移除的位置
        if len(node_list)==1:
           return None
        if remove_loc==0:
           return node_list[0].next
        if remove_loc==len(node_list)-1:
           node_list[-2].next=None
           return node_list[0]
        else:
           node_list[remove_loc-1].next=node_list[remove_loc].next
           return node_list[0]
相关推荐
凌波粒10 小时前
LeetCode--459.重复的子字符串(字符串/KMP算法)
算法·leetcode·职场和发展
_深海凉_10 小时前
LeetCode热题100-移除元素
数据结构·算法·leetcode
米粒110 小时前
力扣算法刷题 Day 36
算法·leetcode·职场和发展
呼啦啦56110 小时前
leetcode练习——栈和队列
算法·leetcode·职场和发展
y = xⁿ12 小时前
【LeetCode】哈希表
算法·leetcode·散列表
北顾笙98012 小时前
day22-数据结构力扣
数据结构·算法·leetcode
人道领域12 小时前
【LeetCode刷题日记】454:四数相加Ⅱ
算法·leetcode
进击的荆棘13 小时前
递归、搜索与回溯——递归
算法·leetcode·递归
小白菜又菜1 天前
Leetcode 2075. Decode the Slanted Ciphertext
算法·leetcode·职场和发展
摸个小yu1 天前
【力扣LeetCode热题h100】链表、二叉树
算法·leetcode·链表