【LeetCode从零单排】No19.RemoveNthNodeFromEndofList
题目
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.
代码
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/public class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode faster = head;ListNode slower = head;while (n > 0 && faster != null) {faster = faster.next;n--;}// Check if has only nodeif (faster == null) return head.next; while (faster.next != null) {faster = faster.next;slower = slower.next;}// Remove slower.next which is the nth form the endslower.next = slower.next.next;return head;}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No19.RemoveNthNodeFromEndofList相关推荐
- 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...
- 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...
- 【LeetCode从零单排】No.7 Reverse Integer
前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目 没想到做的第一道题目,虽然看似 ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【LeetCode从零单排】No221.Maximal Square
题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...
- 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
最新文章
- FPGA之道(67)代码中的约束信息(四)状态机的相关约束
- android之android Studio 安装后打不开的解决方法
- 实操|如何将 Containerd 用作 Kubernetes runtime
- android blcr 编译,BLCR 基本环境搭建【zz~】
- 【OS学习笔记】一 处理器、内存和指令
- OpenCV-Python实战(20)——OpenCV计算机视觉项目在Web端的部署
- SharePoint下利用DocX组件导出Word
- sql server 查询当前月份日期列表数据
- Java下载及环境配置
- vs2018 设置了包含路径还是提示说打不开头文件
- 《DirectX 9.0 3D游戏开发编程基础》-第一篇-VS2010下安装配置DirectX9 runtime和SDK
- Orcle 12c 新特性--- 支持PDB OMF
- thinkadmin默认ckeditor富文本配置修改
- iPhone OS 4.0发布 苹果手机进入多任务时代
- MYSQL数据库更改目录及NAVACATamp;nbs…
- AD封装与原件的一一对应(+封装管理器的使用)
- 安卓平台Flutter启动过程全解析
- 调整oracle服务器时间,oracle数据库因一次服务器时间调整引发的实例宕机注意事项及解决方案...
- 10 Java基础 多线程2
- Java原来还可以这么学:如何搞定面试中必考的集合类
热门文章
- MFC中滚动条slider和编辑框edit的联动
- python泰勒展开_如何利用sympy对未知函数$f(x)$进行符号泰勒展开
- textview 背景变形_重庆新中式床背景品牌
- Windows下Socket库的初始化和关闭
- javamac系统通过pid获取进程名称_线上环境 Linux 系统调用追踪
- html页面vertical,vertical.html
- PHP不及格标红,php语言编写switch判断成绩代码。分别输出优秀、良好、中等、及格和不及格。...
- java子网划分_子网划分讲解及练习(二)
- .net 连接mysql的字符串_asp.net连接数据库字符串
- SYDTEK OTA 蓝牙升级功能实现 附带Demo