LeetCode(46)-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,然后倒数去掉第n个数字,返回head
- 利用双指针,一个first先走n个,然后next在和first同步走,等到first.next != null;这时next正好指向要去掉元素的前一个,next.next = next.next.next;
- 注意去掉head的情况和链表为0和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||head.next == null){
return null;
}
ListNode prev = head;
ListNode next = head;
for(int i = 0;i < n;i++){
prev = prev.next;
}
if(prev == null){
head = head.next;
return head;
}
while(prev.next != null){
prev = prev.next;
next = next.next;
}
next.next = next.next.next;
return head;
}
}
LeetCode(46)-Remove Nth Node From End of List的更多相关文章
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the 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】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】Remove Nth Node From End of List(easy)
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
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- 【JAVA、C++】LeetCode 019 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 ...
- 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 ...
- (链表 双指针) 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 ...
随机推荐
- 02_IO操作的基本规律(InputStream,OutputStream,Reader,Writer,FileReader,FileWriter,BufferedReader,BufferedWri
模拟BufferedInputStream,编写一个类 package toto.IO; import java.io.IOException; import java.io.InputStre ...
- Android实现系统ROOT, 并能赋予app root权限
1. 获取root权限 --> 修改adb源码 a. 打开 system/core/adb/adb_main.cpp,或者是 system/core/adb/daemon/main.c ...
- VS2010 express中改变VC Default include/lib/… 目录
转自: Liz's Blog http://www.cnblogs.com/lizmy/archive/2012/01/10/2318258.html 2010中是以工程为单位,更改VC++ dire ...
- iOS下WebRTC音视频通话(三)-音视频通话
前两篇文章记录了音视频通话的一些概念和一些流程,以及一个局域网内音视频通话的示例. 今天以一个伪真实网络间的音视频通话示例,来分析WebRTC音视频通话的过程. 上一篇因为是在相同路由内,所以不需要穿 ...
- Android初级教程理论知识(第七章服务)
服务两种启动方式 startService:服务被启动之后,跟启动它的组件没有一毛钱关系 bindService:跟启动它的组件同生共死 绑定服务和解绑服务的生命周期方法:onCreate->o ...
- Hessian探究(一)Hessian与springMVC结合
上一篇博客Hessian探究(一)Hessian入门示例我们初步简单的介绍了一下Hessian的使用入门示例,我们是通过Servlet来暴露Hessian的对外服务的,接下来我们介绍一下通过Sprin ...
- Android初级教程理论知识(第一章快速入门)
一.综合介绍. Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R.java:项目中所有资源文件的资源id Android.jar:Android的jar包, ...
- 编译Android 4.4.2源码
在之前的文章中,和大家分享了在天朝下下载android 4.4.2源码的过程(详见下载android4.4.2源码全过程(附已下载的源码)),现在写下编译的笔记. 虽然在android doc中,有提 ...
- Dynamics CRM2011/2013 站点地图sitemap的翻译
实体.属性字段.ribbon等的翻译可以通过解决方案来解决(具体可见我前面的博客:http://blog.csdn.net/vic0228/article/details/37690913),但解决方 ...
- avcodec_decode_video2()解码视频后丢帧的问题解决
使用libav转码视频时发现一个问题:使用下面这段代码解码视频时,视频尾巴上会丢掉几帧. while(av_read_frame(ifmt_ctx,&packet) >= 0){ ret ...