【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 ...
随机推荐
- Windows2012 cannot access netapp CIFS share
NAS1> options cifs.smb2.signing.requiredcifs.smb2.signing.required off NAS1> options cifs.smb2 ...
- DHCP协议讲解
一.DHCP服务介绍: DHCP为动态主机配置协议,该协议能自动配置主机的IP地址.子网掩码.网关及DNS服务器等TCP/IP信息.DHCP可以降低客户机IP地址配置的复杂度和网络管理成本. DHCP ...
- C# 用正则表达式替换字符串中所有特殊字符
descriptionXML = Regex.Replace(ToDBC(descriptionXML.ToUpper().Replace((char)32, ' ').Replace((char)1 ...
- PLSQL_闪回操作1_Flashback Query
2014-07-02 Created By BaoXinjian
- UVA116 单向 DSP(多段图最短路)
单向 DSP [题目链接]单向 DSP [题目类型]dp &题解: 紫书P271 这块的字典序排序我觉得挺厉害的,每次都把那3步sort一下,之后if (v< d[i][j]) 这块的小 ...
- uva 725 Division(暴力模拟)
Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...
- [复变函数]第05堂课 1.4 复球面与 $\infty$; 作业讲解; 2 解析函数 2.1 解析函数的概念与 Cauchy-Riemann 方程
1. 复球面 大漠孤烟直, 长河落日圆. $$\bex \bbC\cong \bbS^2\bs \sed{N},\quad \bbC_\infty=\bbC\cup \sed{\infty}\mbox ...
- eclipse升级,导入旧版eclipse的插件[转]
启动 eclipse.菜单 File => import … => Install => From existing Installation, 点确定, 就会弹出对话框, 浏览选择 ...
- android webview type=file文件上传,安卓端代码
http://stackoverflow.com/questions/5907369/file-upload-in-webview http://blog.csdn.net/longlingli/ar ...
- java多线程之计算数量
package Thread.Abort; import java.util.ArrayList; import java.util.List; import java.util.Random; im ...