Given a linked list, remove the n-th node from the end of list and return its head.

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.

Follow up:

Could you do this in one pass?

 
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pSlow = head;
ListNode pFast = head;
for(int i = 0; i < n; i++){
pFast = pFast.next;
} if(pFast == null){ //remove first node
return head.next;
} while(pFast.next!=null){
pFast = pFast.next;
pSlow = pSlow.next;
}
pSlow.next = pSlow.next.next;
return head; }
}

解题思路:使用快慢指针,实现O(n)遍历。

19. Remove Nth Node From End of List (JAVA)的更多相关文章

  1. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

  2. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  3. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  4. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...

  5. [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 ...

  6. 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, ...

  7. [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, ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 电脑忘记WiFi密码了,但又想知道,该怎么办?

    如何查看电脑已经连过的WiFi的密码? 你有没有遇到这样的情况,电脑之前连过的WiFi,正好手机也想连此WiFi,但是忘记密码了,没有WiFi的手机怎么能叫手机呢?.下面我们来看看如何查看已连接过的W ...

  2. springboot学习五:IDEA外部tomcat发布

    pom.xml配置 屏蔽自带tomcat <dependency> <groupId>org.springframework.boot</groupId> < ...

  3. 走进HashMap

    1.手写实现HashMap 2.解析代码并阐述HashMap1.7到1.8的变化 3.HashMap的遍历方式 4.HashMap与HashSet,Hashtable,ConcurrentHashMa ...

  4. 通过Loadruner对mysql数据库进行增删改查

    操作mysql数据库,是在实现mysql数据源配置的基础上操作,可先阅读:loadrunner参数化使用mysql数据源失败解决方法 写之前先理一下,数据库访问流程:打开数据库  --> 数据库 ...

  5. python汉诺塔问题的递归理解

    一.问题背景 汉诺塔问题是源于印度一个古老传说. 源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下 ...

  6. 关于jfinal发送邮件走过的坑

    最近接到一个写发送邮件的功能开发,使用的是jfinal框架.原本打算使用javamail一步步来的,后来看到jfinal有自带的发邮件的插件(jfinal-mail-plugin),只需两三行代码便可 ...

  7. 一文读懂PRBS定义、生成办法、作用

    对于眼图测试.误码率和抖动容限测试,最常用的测试码是PRBS,主要有PRBS7.PRBS15.PRBS23和PRBS31.本文主要解释了PRBS的定义,生成方法以及简单应用. PRBS定义 二进制序列 ...

  8. webpack代理解决跨域问题

    new WebpackDevServer(webpack(config), { hot:hot, inline: true, compress: true, //去掉真实ip的检测 disableHo ...

  9. 侧脸生成正脸概论与精析(一)Global and Local Perception GAN

    侧脸生成正脸我一直很感兴趣,老早就想把这块理一理的.今天来给大家分享一篇去年的老文章,如果有不对的地方,请斧正. Beyond Face Rotation: Global and Local Perc ...

  10. Python——Django运行问题

    1.Python3.7+Django2.2操作Mysql数据库时出现如下错误:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3. ...