题目来源:

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相关推荐

  1. LeetCode:Remove Nth Node From End of List 移除链表倒第n项

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...

  2. Remove Nth Node From End of List - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Remove Nth Node From End of List - LeetCode 注意点 考虑删除的结点是开头的那个结点 输入给的链表是没有开头的& ...

  3. 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 ...

  4. LeetCode 19. Remove Nth Node From End of List

    LeetCode 19. Remove Nth Node From End of List Solution1:我的答案 并不算是最优解法. /*** Definition for singly-li ...

  5. 数据结构与算法 | Leetcode 19. Remove Nth Node From End of List

    原文链接:https://wangwei.one/posts/jav... 前面,我们实现了 两个有序链表的合并 操作,本篇来聊聊,如何删除一个链表的倒数第N个节点. 删除单链表倒数第N个节点 Lee ...

  6. LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素

    前言 本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素 代码 # -*- coding: utf-8 - ...

  7. 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, ...

  8. 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, ...

  9. 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 ...

  10. 【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 ...

最新文章

  1. Mysql锁专题:InnoDB锁概述
  2. slam中特征点归一化原因以及方法
  3. 怎么把ide改成ahci_如何将硬盘由IDE模式修改为AHCI模式,我的主板是华硕P8Z68-VLX,请高手帮帮忙。...
  4. When Cyber Security Meets Machine Learning 机器学习 安全分析 对于安全领域的总结很有用 看未来演进方向...
  5. 几种人类设计的永动机,最后一个彻底服了!| 今日最佳
  6. Qt如何将数据保存成CSV文件
  7. virtualbox配置apache_virtualbox 网络配置 (转)
  8. c#winform演练 ktv项目 关注MediaPlayer控件的状态
  9. ASP静态HTML(局部)生成类
  10. 专访商汤联合创始人林达华:商汤的开源战略,从算法做起
  11. VUE router-view 页面布局 (嵌套路由+命名视图)
  12. 被低估的电池管理系统BMS
  13. 移远百科 | LTE-A关键技术分析
  14. DNS域名解析服务介绍
  15. Springboot整合JdbcTemplate实现分页查询
  16. 关于activity的生命周期1
  17. Viz-artist常用脚本操作
  18. jsplumb 系列(一)
  19. 重磅!GitHub突然宣布,对全球人免费开放全部核心功能
  20. 砥砺前行!华为构建开放共赢云生态

热门文章

  1. 有必要做 Code Review 吗???
  2. 今天我们来聊一聊 Spring 中的线程安全性
  3. 前后端分离开发,RESTful 接口应该这样设计
  4. SpringBoot实现通用的接口参数校验
  5. 秋招必备:斩获腾讯offer的简历分享!
  6. 陶哲轩之后,有人在这个猜想的证明之路上又前进了一步
  7. 100年前的北京Vlog火了!大神利用AI修复古董纪录片,还原1920年的京城生活
  8. 最新离婚大数据曝光:所有的分手,都是蓄谋已久
  9. 美多后台管理和项目环境搭建
  10. Vue之概述、基本使用、data数据和if条件渲染