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 ...
随机推荐
- 会声会影小成果分享(那段青春岁月)——校学习部宣传视频制作&生日祝福
大二的时候在校学习部当副部长,没有给干事们带去好的工作经验和管理方法,我净在折腾新媒体方面的东西,很惭愧的说,那时候申请了一个微信的公众号(HGXXB1314),我那时候以为自己很叼,最后是发现自己装 ...
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
1 PersonTestMapper.xml中的内容如下: <?xmlversion="1.0"encoding="UTF-8"?> < ...
- 09 ExpanableListView 的代码例子
<span style="font-size:18px;">package com.qf.day09_expandablelistview03; import andr ...
- Dynamics CRM 修改数据导出到EXCEL的最大条数
系统默认的最大导出数为一万,这个数可以通过执行以下SQL看到,那要增加导出的最大数量改变MaxRecordsForExportToExcel的值即可. <span style="fon ...
- UNIX网络编程——套接字选项(心跳检测、绑定地址复用)
/* 设置套接字选项周期性消息检测连通性 心跳包. 心博.主要用于长连接. * 参数:套接字, 1或0开启, 首次间隔时间, 两次间隔时间, 断开次数 */ void setKeepAlive( in ...
- Java中的this关键字
//Java中的this //this引用---->当前对象的引用 /* * 1.this在什么地方(非静态的)访问另外一个成员(非静态,可以是属性或者方法) * 前面都省略了this * 2. ...
- IMX51---GPIO
GPIO(General Purpose Input/Output)指通用输入/输出,IMX51的GPIO模块提供32位双向的.通用输入和输出的信号,下图是GPIO的框图: 图1 1. GP ...
- LocalBroadcastManager—创建更高效、更安全的广播
前言 在写Android应用时候,有时候或多或少的需要运用广播来解决某些需求,我们知道广播有一个特性,就是使用sendBroadcast(intent);发送广播时,手机内所有注册了Broadcast ...
- 【Unity Shaders】Reflecting Your World —— 在Unity3D中创建一个简单的动态Cubemap系统
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- python的文件操作file:(内置函数,如seek、truncate函数)
file打开文件有两种方式,函数用file()或者open().打开后读入文件的内容用read()函数,其读入是从文件当前指针位置开始,所以需要控制指针位置用: 一.先介绍下file读入的控制函数: ...