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,
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相关推荐
- [LeetCode]Remove Nth Node From End of List
又一道链表题.CareerCup上也有类似题目,trick和"部分链表逆序"一样,由于不知道链表的长度,second指针比first指针先走n步,然后在同时向前移动,直到secon ...
- 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 Remove Nth Node From End of List
题意:给出一个单链表,删除从尾部开始的第n个元素 思路: 从头开始遍历n个元素,此时当前结点指向头开始的第n+1个元素.继续遍历同时,另外一个结点记录从头开始至遍历结束时的所经历的结点,这个结点就是要 ...
- 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:Remove Nth Node From End of List 移除链表倒第n项
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...
- 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 - ...
最新文章
- vue-i18n国际化实例
- 网站基本建设必备窍门了解一下!
- SAP Spartacus B2B功能,只渲染BodyContent position里的UI
- linux+vim+动不了,linux的vim按了ctrl+s之后假死的解决办法
- 使用MybatisPlus在实体中添加数据库表中不存在的字段
- 数据库备份与恢复 之四 选择数据库还原方案
- 什么导致了android.os.NetworkOnMainThreadException异常
- 基于Springboot的医院药品管理系统的设计与实现.zip(论文+项目源码)
- 使用J-link+J-Flash给STM32芯片烧写序列号
- 记一次调试YOLOv5+DeepSort车辆跟踪项目的经过
- 360奇酷手机显示Log
- IP电话系统和VoIP系统使用指南
- 月报总结|2月份Moonbeam最新进展
- Day 41多表查询以及pymysql相关操作 完善
- 贷还是不贷:如何用 Python 和机器学习帮你决策?
- ZYNQ图像处理|静态图像通路|VDMA寄存器、DDR内存操作
- [bzoj1812][ioi2005]riv(树上dp)
- 华清远见嵌入式开发学习的6大阶段
- 最简单安装使用peda
- 【ntp】ntpdatentpd