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. 浏览器渲染页面的时候,不同的script块之间的关系

    浏览器渲染页面时,当读到script元素的时候,浏览器中的js引擎会分多个script代码块来读取,不同的script代码出错互不影响,但是由于script中的变量作用域是全局,所以前面代码块声明的变 ...

  2. log4j2 实际使用详解

    转载至: https://blog.csdn.net/vbirdbest/article/details/71751835 如下是maven项目中的实例: 首先pom.xml中引入如下依赖,注意看都是 ...

  3. Angular中的服务的使用

    定义公共的方法,使得方法在组件之间共享调用 1. 创建服务命令 ng g service modbus-service # 创建服务到指定目录下 ng g service services/modbu ...

  4. 数学模块_math

    ceil 进一, 向上取整 floor 向下取整 pow(x, y) x的y次方 print(math.pow(2, 3)) # 8.0 sqrt(x) x的开平方(结果为浮点数) print(mat ...

  5. jenkins使用git拉取gitlab代码

    1 在安装jenkins的主机上新加一个jenkins用户, 切换到jenkins用户登录, 生成公钥私钥ssh-keygen -t rsa -C "your email" -f ...

  6. peewee 通俗易懂版

    Peewee作为Python ORM之一 优势:简单,小巧,易学习,使用直观 不足之处:需要手动创建数据库 基本使用流程 1⃣️根据需求定义好Model(表结构类) 2⃣️通过create_table ...

  7. bootstrap的使用集锦

    在使用div样式的时候可以根据页面布局来调整大小 <div class="col-md-8 col-md-offset-3"># col-md-8 div所占的空间大小 ...

  8. 【perl】企业微信发消息

    https://open.work.weixin.qq.com/api/doc#90000/90135/90236 #!/usr/bin/env perl use strict; use warnin ...

  9. loadrunner参数化使用mysql数据源失败解决方法

    操作系统:win7 在64位的操作系统上,如果你想要连接32位mysql,避免安装mysql connector/odbc 64位,否则即使配置ODBC数据源连接正常,但loadrunner无法正常调 ...

  10. Burpsuite Sqlmap Nmap入门总结

    burpsuite sqlmap nmap 简介 sqlmap基础 五种独特sql注入技术: 基于布尔类型的盲注 基于时间的盲注 基于报错注入 联合查询注入 堆查询注入 sqlmap入门 1.判断是否 ...