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 ...
随机推荐
- C语言-人狼羊菜问题-最容易看懂的解决方法及代码
题目描述:农夫需要把狼.羊.菜和自己运到河对岸去,只有农夫能够划船,而且船比较小,除农夫之外每次只能运一种东西,还有一个棘手问题,就是如果没有农夫看着,羊会偷吃菜,狼会吃羊.请考虑一种方法,让农夫能够 ...
- 【通信】Netty JBOSS提供的一个java开源框架
Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序dsf. 也就是说,Netty 是一个基 ...
- vim中编写python代码使用python-mode和syntastic插件时警告(Warning)的消除
问题: 在Vim使用了syntastic后,编写代码时,可以对代码错误和警告进行相对实时的了解,对编写代码有很大的帮助.同时这个插件和python-mode一起工作时,可以对python代码的编写提供 ...
- struts2拦截器-简单实现非法登录验证
概念:什么是拦截器 拦截器实现了面向切面的组件,它会影响多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现高度内聚,确保业务对象的整洁! 为什么使用拦截器 拦截器消除了动作 ...
- Java-使用js进行编码,后台解码。
1:使用js编码 var value=window.encodeURI(window.encodeURI(strValue)); 2:Java类中解码. String str=URLDecoder.d ...
- Recommender Systems 基于知识的推荐
前两章的协同过滤和就内容的推荐都建立在“大量数据”的基础上,运用概率方法来进行计算和预测.不过,在现实生活中,有些物品,如:汽车.房屋.计算机,用户不会频繁的消费.如何在这种情况下对用户进行推荐? 这 ...
- centos64位安装32位C/c++库
yum install glibc.i686 glibc-devel.i686 yum install libstdc++.i686yum install libstdc++-devel.i686yu ...
- Chp2: Linked List
2.2 Implement an algorithm to find the kth to last element of a singly linked list. Just using " ...
- eclipse查看jar包中class的中文注释乱码问题的解决
1,问题来源是在eclipse中直接查看springside的class(由eclipse自动反编译)里面注释的乱码问题: Preferences-General-Workspace-Text fil ...
- Linux系统下如何配置SSH?如何开启SSH?
SSH作为Linux远程连接重要的方式,如何配置安装linux系统的SSH服务,如何开启SSH?下面来看看吧(本例为centos系统演示如何开启SSH服务). 查询\安装SSH服务 1.登陆linux ...