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 ...
随机推荐
- 利用IDE编写C语言程序的一点注意事项
前言:我是喜欢编程的一只菜鸟,在自学过程中,对遇到的一些问题和困惑,有时虽有一点体会感悟,但时间一长就会淡忘,很不利于知识的积累.因此,想通过博客园这个平台,一来记录自己的学习体会,二来便于向众多高手 ...
- cron服务 ubuntu
linux 定时执行crontab crontab -e 进入一个vi 编辑界面 在最后一行加上 */30 * * * * netstat > /tmp/net.log 表示每隔30分就执行n ...
- 让用户打开你app的位置功能
+运动 http://www.ccidnet.com/2015/0819/10014152.shtml 让你的app不再是一个购物网站, 而是一种生活方式, 逛街,在实体店逛街积累里程,兑换积分 送 ...
- Lua基础之coroutine(协程)
概括:1.创建协程2.coroutine的函数3.coroutine的基本流程4.yield对coroutine流程的干预5.resume, function()以及yield之间的参数传递和返回值传 ...
- 简单3d RPG游戏 之 005 选择敌人
选择一个敌人,按ctrl+d,复制出3个,调整一下它们的位置,不重叠,修改Tag为Enemy,禁用EnemyAI. 创建Targetting脚本,绑定到Player玩家对象 public class ...
- spring的配置模式与注解模式基础
“依赖注入”是spring的核心特征,在Web服务器(如Tomcat)加载时,它会根据Spring的配置文件中配置的bean或者是通过注解模式而扫描并装载的bean实例自动注入到Application ...
- hibernate中session
hibernate中的session是一级缓存,可以理解为进程级的缓存.在进程运行期间一直存在. session可以理解为一个可以操作数据库的对象 具体如何操作数据库? session中有方法, 如果 ...
- hibernate多对一单向外键
hibernate多对一单向外键: 描述:
- ***用php的strpos() 函数判断字符串中是否包含某字符串的方法
判断某字符串中是否包含某字符串的方法 if(strpos('www.idc-gz.com','idc-gz') !== false){ echo '包含'; }else{ echo '不包含'; } ...
- WPF之小动画一
定义动画: 直接使用Element进行BeginAnimation DoubleAnimation animation = new DoubleAnimation(); animation.By = ...