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]
相关推荐
爱coding的橙子42 分钟前
每日算法刷题Day41 6.28:leetcode前缀和2道题,用时1h20min(要加快)
算法·leetcode·职场和发展
前端拿破轮3 小时前
不是吧不是吧,leetcode第一题我就做不出来?😭😭😭
后端·算法·leetcode
前端拿破轮4 小时前
😭😭😭看到这个快乐数10s,我就知道快乐不属于我了🤪
算法·leetcode·typescript
今天背单词了吗9809 小时前
算法学习笔记:4.KMP 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
笔记·学习·考研·算法·leetcode·kmp算法
hn小菜鸡15 小时前
LeetCode 377.组合总和IV
数据结构·算法·leetcode
亮亮爱刷题10 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
zmuy10 天前
124. 二叉树中的最大路径和
数据结构·算法·leetcode
chao_78910 天前
滑动窗口题解——找到字符串中所有字母异位词【LeetCode】
数据结构·算法·leetcode
Alfred king10 天前
面试150跳跃游戏
python·leetcode·游戏·贪心算法
呆呆的小鳄鱼10 天前
leetcode:746. 使用最小花费爬楼梯
算法·leetcode·职场和发展