【LeetCode】19. Remove Nth Node From End of List
题目:

思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1。
1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表
扫过的节点依次:1-2-3
n值得变化:3-2-1
2.链表1->2->3,n=3
扫过的节点依次:1-2-3
n值得变化:2-1-0
3.链表1->2->3,n=2
扫过的节点依次:1-2-3
n值得变化:1-0--1
当走到链表结尾时:1.n>0,说明链表根本没有第n个节点,直接返回原链表;
2.n=0,说明链表倒数第n个节点就是头结点,返回head.next;
3.n<0,重新从头结点开始走,没移动一步让n加1,当n=0时,移动停止,当前移动到的节点就是要删除节点的前一个节点。因为如果链表长度是L,则倒数第n个节点的前一个结点就是L-n。第一次扫到链表末尾时,n的值变成n-L,当n不断加1直到为0时,第二次扫到的位置正好是第L-n个节点处。
/**
* 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||n<1){
return head;
}
ListNode cur=head;
while(cur!=null){
n--;
cur=cur.next;
}
if(n==0){
return head.next;
}
if(n<0){
cur=head;
while(++n!=0){//当n=0时移动停止,移动到的节点就是要删除节点的前一个节点
cur=cur.next;
}
cur.next=cur.next.next;
}
return head;
}
}
【LeetCode】19. Remove Nth Node From End of List的更多相关文章
- 【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 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
- 【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 ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- LeetCode题解(19)--Remove Nth Node From End of List
https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the ...
- LeetCode OJ 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❤python】 19. Remove Nth Node From End of List
#-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...
- [Leetcode][Python]19: Remove Nth Node From End of List
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...
随机推荐
- 【转】libvirt kvm 虚拟机上网 – Bridge桥接
libvirt kvm 虚拟机上网 – Bridge桥接 2013 年 7 月 3 日 / 东东东 / 暂无评论 目录 [hide] 1 Bridge桥接原理 2 在host机器配置桥接网络 2.1 ...
- MySQL计算时间差
MySQL计算两个日期的时间差函数:TIMESTAMPDIFF 语法: TIMESTAMPDIFF(interval, datetime_expr1, datetime_expr2) interval ...
- ab压测参数说明
ab是apache自带的压力测试工具,非常实用.ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也 ...
- VBA标准模块与类模块
大家通过之前的介绍,已知道怎么将一个空模块插入VBA的工程中.从插入模块中可以看到,模块有有两种——标准模块与类模块.类模块是含有类定义的特殊模块,包括其属性和方法的定义.在后面会有介绍与说明. 随着 ...
- sublime text 3 安装注释
sublime text 3 1.安装Sublime Text 3 下载安装:http://www.sublimetext.com/3 Package Control安装:https://subli ...
- 206. Reverse Linked List
反转链表 注意是借用 假的头节点,这样算法判断开始和结束,就好很多了. 借用头插法. []dummy/head [] [] [] [] head curr ==== class Solution ...
- 黄聪:wordpress工作原理
WP初始化的过程:当你输入<yourlink>/wordpress对wordpress进行初始化时,wordpress默认会找根目录下的index.php页面,看一下index.php页面 ...
- 四层LB和七层LB
总结: 基于MAC地址玩的是二层(虚拟MAC地址接收请求,然后再分配到真实的MAC地址), 基于IP地址玩的是三层(虚拟IP地址接收请求,然后再分配到真实的IP地址), 基于IP地 ...
- lower_bound实现函数
lower_bound实现 [参考链接]lower_bound二分的三种写法 我在以前,总是用lower_bound,现在发现这样不行,有些复杂的数据结构二分的时候用这个会很麻烦,不如手写二分,我接着 ...
- CMake使用教程
转自 RichardXG 原文 CMake使用教程 CMake是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目. 通过编写CMak ...