题目链接

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

主要难点是通过一趟遍历寻找链表倒数第k个元素,具体见代码注释                            本文地址

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *removeNthFromEnd(ListNode *head, int n) {
12         //快指针先走n步,然后快慢指针一起走,块指针指向尾节点时,慢指针指向倒数第n个节点
13         ListNode* fast = head, *slow = head, *slowpre = NULL;
14         for(int i = 1; i < n; i++)fast = fast->next;
15         while(fast->next)
16         {
17             fast = fast->next;
18             slowpre = slow;
19             slow = slow->next;
20         }
21         if(slow == head)
22             head = head->next;
23         else
24             slowpre->next = slow->next;
25         delete slow;
26         return head;
27     }
28 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3667510.html

转载于:https://www.cnblogs.com/TenosDoIt/p/3667510.html

LeetCode:Remove Nth Node From End of List相关推荐

  1. [LeetCode]Remove Nth Node From End of List

    又一道链表题.CareerCup上也有类似题目,trick和"部分链表逆序"一样,由于不知道链表的长度,second指针比first指针先走n步,然后在同时向前移动,直到secon ...

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

  3. LeetCode Remove Nth Node From End of List

    题意:给出一个单链表,删除从尾部开始的第n个元素 思路: 从头开始遍历n个元素,此时当前结点指向头开始的第n+1个元素.继续遍历同时,另外一个结点记录从头开始至遍历结束时的所经历的结点,这个结点就是要 ...

  4. Remove Nth Node From End of List - LeetCode

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

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

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

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

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

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

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

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

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

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

最新文章

  1. vue-i18n国际化实例
  2. 网站基本建设必备窍门了解一下!
  3. SAP Spartacus B2B功能,只渲染BodyContent position里的UI
  4. linux+vim+动不了,linux的vim按了ctrl+s之后假死的解决办法
  5. 使用MybatisPlus在实体中添加数据库表中不存在的字段
  6. 数据库备份与恢复 之四 选择数据库还原方案
  7. 什么导致了android.os.NetworkOnMainThreadException异常
  8. 基于Springboot的医院药品管理系统的设计与实现.zip(论文+项目源码)
  9. 使用J-link+J-Flash给STM32芯片烧写序列号
  10. 记一次调试YOLOv5+DeepSort车辆跟踪项目的经过
  11. 360奇酷手机显示Log
  12. IP电话系统和VoIP系统使用指南
  13. 月报总结|2月份Moonbeam最新进展
  14. Day 41多表查询以及pymysql相关操作 完善
  15. 贷还是不贷:如何用 Python 和机器学习帮你决策?
  16. ZYNQ图像处理|静态图像通路|VDMA寄存器、DDR内存操作
  17. [bzoj1812][ioi2005]riv(树上dp)
  18. 华清远见嵌入式开发学习的6大阶段
  19. 最简单安装使用peda
  20. 【ntp】ntpdatentpd

热门文章

  1. 枚举很好用啊,为啥阿里不建议返回值用枚举?
  2. 哥们,你真以为你会做这道JVM面试题?
  3. 机器学习数学基础:常见分布与假设检验
  4. 用AI还原李焕英老照片动态影像
  5. 重磅!2020 中国高校毕业生月薪排名:清华第一,24所高校过万
  6. 又现神论文!《本人娶刘亦菲的可行性报告》省级期刊收录
  7. 别再@微信官方 了,我给你一面小国旗!
  8. 【每日一算法】相同的树
  9. Django源码分析6:auth认证及登陆保持
  10. python字符串用法详解(str、下标、切片、查找、修改、判断)