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]
相关推荐
Pluchon5 小时前
硅基计划4.0 算法 二叉树深搜(DFS)
java·数据结构·算法·leetcode·深度优先·剪枝
sprintzer6 小时前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展
一匹电信狗7 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
学学学无无止境8 小时前
力扣-上升的温度
leetcode
Emilia486.10 小时前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
Dream it possible!11 小时前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
程序员阿鹏11 小时前
49.字母异位词分组
java·开发语言·leetcode
Espresso Macchiato12 小时前
Leetcode 3715. Sum of Perfect Square Ancestors
算法·leetcode·职场和发展·leetcode hard·树的遍历·leetcode 3715·leetcode周赛471
Miraitowa_cheems18 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号18 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode