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]
相关推荐
longlongzihan12 小时前
从LeetCode 70. 爬楼梯看动态规划:如何精准推导状态转移方程?
c++·算法·leetcode·动态规划
Rnan-prince17 小时前
LeetCode 股票买卖系列:一个动态规划状态机模板通吃四题
leetcode·动态规划
圣保罗的大教堂18 小时前
leetcode 3756. 连接非零数字并乘以其数字和 II 中等
leetcode
All for pursuit.1 天前
【回溯-7】494.目标和
数据结构·c++·算法·leetcode
Tisfy1 天前
LeetCode 0678.有效的括号字符串:O(n)+O(1)一次遍历
算法·leetcode·字符串·题解·贪心·括号匹配
Navigator_Z2 天前
LeetCode //C - 1278. Palindrome Partitioning III
c语言·算法·leetcode
青山木2 天前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
All for pursuit.2 天前
【回溯-4】46.全排列
数据结构·c++·算法·leetcode
All for pursuit.2 天前
【回溯-5】78.子集
数据结构·c++·算法·leetcode
代码什么用2 天前
LeetCode-560.和为 K 的子数组
算法·leetcode