[LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点
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?
题目要求删除一个节点,但是给出的是倒数的节点,如果是正数的节点一次遍历过去就可以了,但是倒数的节点似乎不能这么做。
其实还是可以一次遍历删除节点,我们先设置一个指针i,往前推n个,这个时候再设置第二个指针j,两个指针i和j同时往前推,这样当
i抵达链表结尾时j的位置正好是倒数第n个节点,此时把节点j删除就可以了(测试样例中有个特殊例子,输入[1],这里要特殊化处理)
/**
* 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 res=new ListNode(0);
res.next=head;
ListNode l1=res;
ListNode l2=res;
for(int i=1;i<=n+1;i++){
l1=l1.next;
}
while(l1!=null){
l2=l2.next;
l1=l1.next;
}
l2.next=l2.next.next;
return res.next;
}
}
[LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点的更多相关文章
- 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)
这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- 019 Remove Nth Node From End of List 删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点并返回头结点.例如,给定一个链表: 1->2->3->4->5, 并且 n = 2.当删除了倒数第二个节点后链表变成了 1->2 ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)
题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了 2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...
- Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- 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 Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
随机推荐
- loj#6229. 这是一道简单的数学题 (??反演+杜教筛)
题目链接 题意:给定\(n\le 10^9\),求:\(F(n)=\sum_{i=1}^n\sum_{j=1}^i\frac{\mathrm{lcm}(i,j)}{\mathrm{gcd}(i,j)} ...
- Java技术列表
完整的java技术列表,可以在oracle官网找到: https://www.oracle.com/technetwork/java/javaee/tech/index.html JSR: Java ...
- HIbernate | 简单对象嵌套实践
src/entity/Course.java public class Course { private Integer courseNo; private String courseName; pu ...
- 【笔记】Pandas分类数据详解
[笔记]Pandas分类数据详解 Pandas Pandas分类数据详解|轻松玩转Pandas(5) 参考:Pandas分类数据详解|轻松玩转Pandas(5)
- sharepoint_study_9
描述:sharepoint2013 网站修改导航条标题 SuiteBartext 图示: 解决: 管理员身份进sharepoint powershell ,依次敲入搞定1. $app = Get-SP ...
- python3 sorted()函数笔记
import randoma=[]for i in range(9): b=random.randint(0,9)#生产9个随机数 a.append(b)#把生成的随机数添加到列表里面print(so ...
- 4.AOP
1.代理模式 代理模式(Proxy Pattern)是GoF23种常用设计模式之一使用代理模式创建代理对象,让代理对象控制目标对象的访问,并且可以在不改变目标对象的情况下添加一些额外的功能包括静态代理 ...
- Vuejs 实现权限管理
程序运行时,router只配置登陆 首页404 等基本页面 import Main from '@/views/Main.vue'; // 不作为Main组件的子页面展示的页面单独写,如下 expor ...
- linux下重启php服务
有时候修改了一些php配置或者进程满了需要重启php [root@snoopy :: bin]# service php-fpm restart Gracefully shutting down ph ...
- File "<ipython-input-20-ac8d4b51998e>"
环境:Python 3.6 word = "Jesse" ") File "<ipython-input-20-ac8d4b51998e>" ...