19. Remove Nth Node From End of List (JAVA)
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)的更多相关文章
- 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. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【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 ...
- [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 ...
- 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, ...
- [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, ...
- 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 ...
- 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 ...
随机推荐
- matlab画台风轨迹图小tip
<写在前面> 关于台风轨迹图,这次由于作业需要就画了一幅轨迹图,参考原图选自论文: LIU Zenghong, XU Jianping, SUN Chaohui, WU Xiaofen. ...
- Google SketchUp Cookbook: (Chapter 1) Making Multiple Copies
软件环境 SketchUp Pro 2018 参考书籍 Google SketchUp Cookbook http://shop.oreilly.com/product/9780596155100.d ...
- analyse idoc by creation date
t-code ZMM0127 infoset: ZMM_IDOC_READ_01 go to code: AUTHORITY-CHECK OBJECT 'S_IDOCMONI'ID 'ACTVT' F ...
- @property的使用
1.可以将某个函数变为属性 class Name(): @property def name(self): print('xiaoming') Name().name这里name已经可以当做属性来调用 ...
- ubuntu16.04下latex环境搭建
背景: 最近需要使用latex做一下简历~~~ 工具: sublime text3 (plugin: latextools) + texlive-full 配置: Preferences -> ...
- egret编译 FATAL ERROR: CALL_AND_RETRY_0 Allocation failed process out of memory解决
egret 白鹭编译时异常提示: FATAL ERROR: CALL_AND_RETRY_0 Allocation failed process out of memory. 编译时内存溢出, 因为 ...
- 整理Xen理论知识
XEN 简介 XEN 是一个基于X86架构.发展最快.性能最稳定.占用资源最少的开源虚拟化技术.Xen可以在一套物理硬件上安全的执行多个虚拟机,与 Linux 是一个完美的开源组合,Novell SU ...
- java中几种常用的设计模式
参考https://blog.csdn.net/jiyang_1/article/details/50110931 参考https://blog.csdn.net/dean_hu/article/de ...
- Django基础模板层(75-76)
Django框架之模板层(d75)一 模版语法之变量: ** locals() 会把*该*视图函数内的变量,传到模板 ** return render(request, 'index.html' ...
- 解决wxParse空格不解析的问题
遇到的问题: 相似问题:https://blog.csdn.net/qq_41619741/article/details/85774865 http://html51.com/info-41786- ...