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.
思路:
三个指针p1,p2,p3,让第一指针p1走n - 1步停下来,然后p1和p2一起往前走,同时保持一个指针指向p2的前一个节点。
package list;public class RemoveNthNodeFromEndOfList {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode p1 = head;ListNode p2 = head;ListNode prevp2 = new ListNode(0);prevp2.next = p2;head = prevp2;while (--n > 0) {p1 = p1.next;}while (p1.next != null) {p1 = p1.next;prevp2 = p2;p2 = p2.next;}prevp2.next = p2.next;return head.next;}public static void main(String[] args) {// TODO Auto-generated method stub }}
转载于:https://www.cnblogs.com/null00/p/5060233.html
LeetCode - Remove Nth Node From End of List相关推荐
- 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
又一道链表题.CareerCup上也有类似题目,trick和"部分链表逆序"一样,由于不知道链表的长度,second指针比first指针先走n步,然后在同时向前移动,直到secon ...
- 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 - ...
最新文章
- TS - 处理故障的一些通用方法
- Linux检查镜像,Shell脚本实现检测Cygwin最快的镜像站点
- Embedding技术在房产推荐中的应用
- OFFICE——Word与Excel交互处理——邮件合并
- web前端表格css三个t的使用(thead,tbody,tfoot)
- python 量化交易_基于Python的量化交易工具清单(上)
- mysql select in 不存在返回0_MySQL索引优化看这篇文章就够了!
- Pandas DataFrame 去重
- c++ 输出二进制_Q音直播编译优化与二进制集成方案
- Excel2010学习笔记(二):公式函数篇
- spring boot ---- jpa连接和操作mysql数据库
- uva 10817 - Headmaster's Headache ( 状态压缩dp)
- scratch---植物大战僵尸游戏实现,完整代码分享,多种僵尸与植物特效都有素材包!
- 2019年计算机设计大赛(省赛)
- PayPal提现银行不给入账要退回?最新解决方法如下!!!
- 关于《微习惯》的记录
- cocos2d实现画笔功能及常用的绘制函数
- 正在移除icloud数据久_深度梳理一下iOS照片(相册)的逻辑,和减少iCloud的空间占用的方法...
- 武汉星起航跨境电商——亚马逊日本站JCT政策将实现改革
- DIY智能家居语音助理——语音智控万物
热门文章
- python3安装教程linux_python 在linux系统的安装教程
- 【算法】剑指 Offer 57. 和为s的两个数字
- 【Elasticsearch】ElasticSearch 7.8 多字段权重排序
- 【Elasticsearch】 es 集群发现机制
- 【netty】Flink Clickhouse 写入失败 Channel output shutdown Broken pipe
- 【Flink】数据传输 挖个坑 把自己埋了 ClassCastException String cannot be cast to [LJava.lang.String
- 【MySQL】MySQL drop,truncate,delete 区别
- Spark: Structured + hive(Jdbc方式)卡死
- 【Flink】Flink的窗口触发器 PurgingTrigger
- Mac使用终端压缩加密文件