[LeetCode]题解(python):019-Remove Nth Node From End of List
题目来源:
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
题意分析:
这道题是给定一个链表,删除倒数第n个节点。提醒,1.输入的链表长度必然大于n,2.尽量通过访问一次就得到结果。
题目思路:
这道题的问题在于如何找到倒数第n个节点。由于只能访问一次,所以可以建立两个链表,tmp1和tmp2。tmp1先访问第一个到n个节点。这时候,tmp2从0开始访问。等tmp1访问结束的时候,tmp2刚好访问到倒数第n个节点。
代码(python):
1 # Definition for singly-linked list. 2 # class ListNode(object): 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution(object): 8 def removeNthFromEnd(self, head, n): 9 """ 10 :type head: ListNode 11 :type n: int 12 :rtype: ListNode 13 """ 14 ans = ListNode(0); 15 ans.next = head 16 tmp1 = ans 17 tmp2 = ans 18 i = 0 19 while i < n: 20 tmp1 = tmp1.next 21 i += 1 22 while tmp1.next: 23 tmp1 = tmp1.next 24 tmp2 = tmp2.next 25 tmp2.next = tmp2.next.next 26 return ans.next 27
View Code
转载请注明出处:http://www.cnblogs.com/chruny/p/4844007.html
转载于:https://www.cnblogs.com/chruny/p/4844007.html
[LeetCode]题解(python):019-Remove Nth Node From End of List相关推荐
- LeetCode:Remove Nth Node From End of List 移除链表倒第n项
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...
- Remove Nth Node From End of List - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Remove Nth Node From End of List - LeetCode 注意点 考虑删除的结点是开头的那个结点 输入给的链表是没有开头的& ...
- LeetCode算法入门- Remove Nth Node From End of List -day17
LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...
- LeetCode 19. Remove Nth Node From End of List
LeetCode 19. Remove Nth Node From End of List Solution1:我的答案 并不算是最优解法. /*** Definition for singly-li ...
- 数据结构与算法 | Leetcode 19. Remove Nth Node From End of List
原文链接:https://wangwei.one/posts/jav... 前面,我们实现了 两个有序链表的合并 操作,本篇来聊聊,如何删除一个链表的倒数第N个节点. 删除单链表倒数第N个节点 Lee ...
- LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素
前言 本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素 代码 # -*- coding: utf-8 - ...
- Remove Nth Node From End of List leetcode java
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- LeetCode:Remove Nth Node From End of List
题目链接 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- LeetCode上删除链表末尾第N个节点算法——Remove Nth Node From End of List
1.题目 Given a linked list, remove the n-th node from the end of list and return its head. Example: Gi ...
- 【LeetCode】19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
最新文章
- Mysql锁专题:InnoDB锁概述
- slam中特征点归一化原因以及方法
- 怎么把ide改成ahci_如何将硬盘由IDE模式修改为AHCI模式,我的主板是华硕P8Z68-VLX,请高手帮帮忙。...
- When Cyber Security Meets Machine Learning 机器学习 安全分析 对于安全领域的总结很有用 看未来演进方向...
- 几种人类设计的永动机,最后一个彻底服了!| 今日最佳
- Qt如何将数据保存成CSV文件
- virtualbox配置apache_virtualbox 网络配置 (转)
- c#winform演练 ktv项目 关注MediaPlayer控件的状态
- ASP静态HTML(局部)生成类
- 专访商汤联合创始人林达华:商汤的开源战略,从算法做起
- VUE router-view 页面布局 (嵌套路由+命名视图)
- 被低估的电池管理系统BMS
- 移远百科 | LTE-A关键技术分析
- DNS域名解析服务介绍
- Springboot整合JdbcTemplate实现分页查询
- 关于activity的生命周期1
- Viz-artist常用脚本操作
- jsplumb 系列(一)
- 重磅!GitHub突然宣布,对全球人免费开放全部核心功能
- 砥砺前行!华为构建开放共赢云生态