Java [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,
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.
解题思路:
设置两个指针,两个指针相隔n-1,然后两个指针同时向后移动,当后一个指针没有后继节点了,那么前一个指针指向的节点就是需要删除的节点。
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null)
return null;
ListNode pPre = null;
ListNode p = head;
ListNode q = head;
for(int i = 0; i < n - 1; i++)
q = q.next;
while(q.next != null){
pPre = p;
p = p.next;
q = q.next;
}
if(pPre == null)
return head.next;
pPre.next = p.next;
return head;
}
}
Java [leetcode 19]Remove Nth Node From End of List的更多相关文章
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- 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 ...
- (链表 双指针) leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [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 ...
- [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 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 ...
随机推荐
- showModalDialog 超过问题
a.aspx页面打开一个弹出模式对话框b.aspx. a.aspx 页面页面代码: function SetPlay() { window.showModalDialog('SetAdvertisin ...
- wancms从apache迁移至nginx
首先解决nginx1.2不支持pathinfo问题,见上一篇博文 其次是数据库的用户名变了之后,修改各种配置,wancms的,ucenter的,bbs的 还有一个是wacms的后台站点管理里面的uc配 ...
- linux配置JDK(转载)
转载自:http://blog.csdn.net/xinxin19881112/article/details/46816385 Linux CentOS 6.6安装JDK1.7 目录 1.下载JDK ...
- MongoDB安装,启动,注册为windows系统服务
MongoDB安装与启动 周建旭 2014-08-10 解压完后配置环境变量 下载Windows 32-bit或64-bit版本并解压缩,程序文件都在bin目录中,其它两个目录分别是C++调用是的头文 ...
- apache本地和局域网访问设置
apache本地和局域网访问设置 最近做项目需要同事ajax发项目给我,因为是测试环境,所以需要能访问我的服务器.我服务器直接用的wampserver.因为没有接触过,所以百度了一下,都是没有成功,后 ...
- 改变navigationbar 标题颜色
navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor yel ...
- C#和Js 编码和解码方法
Server.UrlDecode(); Server.UrlEncode(); Server.HtmlDecode(); Server.HtmlEncode();
- WDS无线桥接
因为放假回家,长时间不在家,家里也没什么人,所以也就没有网可以用.为了两个月办宽带又不值得,太过浪费了.于是就只能蹭网用了.当然,要和邻居打个招呼或者你能搞定密码的情况下不打招呼(嘿嘿...).但是有 ...
- 优化SQL Server数据库查询方法
SQL Server数据库查询速度慢的原因有很多,常见的有以下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列 ...
- 关闭MyEclipse的Quick Update
关闭MyEclipse的Quick Update, Windows > Preferences > MyEclipse > Community Essentials, 把选项 &qu ...