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]
相关推荐
圣保罗的大教堂9 小时前
leetcode 835. 图像重叠 中等
leetcode
wabs66613 小时前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树
hanlin0313 小时前
刷题笔记:力扣第41题-缺失的第一个正数
笔记·算法·leetcode
0+11115 小时前
算法 --滑动窗口
c++·算法·leetcode
木井巳17 小时前
【记忆化搜索】最长递增子序列
java·算法·leetcode·深度优先·剪枝·推荐算法
圣保罗的大教堂18 小时前
leetcode 1401. 圆和矩形是否有重叠 中等
leetcode
hanlin0319 小时前
刷题笔记:力扣第76题-最小覆盖子串
笔记·算法·leetcode
随便发挥1 天前
Leetcode 剑指 Offer II 186. 文物朝代判断
leetcode
6Hzlia1 天前
【Classic 150 刷题计划】 LeetCode 205. 同构字符串 | C++ 双向绑定与哈希表映射
c++·算法·leetcode
午彦琳1 天前
2026.9.17
数据结构·算法·leetcode