题目:

思路:如果链表为空或者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的更多相关文章

  1. 【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 ...

  2. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  3. 【一天一道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 ...

  4. 【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 ...

  5. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  6. 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  ...

  7. 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 ...

  8. 【leetcode❤python】 19. Remove Nth Node From End of List

    #-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...

  9. [Leetcode][Python]19: Remove Nth Node From End of List

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...

随机推荐

  1. 剑指offer系列46---和为s的连续正数序列

    [题目]输出所有和为S的连续正数序列.序列为:1,2,3,4,5,6,7,8................ * 序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序 package com.e ...

  2. bootstrap的datetimepicker只选择月份

    本文转载自:http://blog.csdn.net/feng1603/article/details/41869523 直接上代码: //选择年月日的 startView: 2, minView: ...

  3. 【Struts2学习笔记-6--】Struts2之拦截器

    简单拦截器的使用 拦截器最基本的使用: 拦截方法的拦截器 拦截器的执行顺序 拦截结果的监听器-相当于 后拦截器 执行顺序: 覆盖拦截器栈里特定拦截器的参数 使用拦截器完成-权限控制 主要完成两个功能: ...

  4. Puppet's Commands 3.7

    Puppet's Commands Puppet’s command line interface consists of a single puppet command with many subc ...

  5. 【Linux】之shell特殊变量整理

    目录 1. 特殊变量列表 2. 特殊说明 在shell中变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即p ...

  6. android fragment getActivity()为空的另一个可能

    目前这个方法得到空指针一般来说是因为Activity被销毁导致无法获取,但是开发中又出了一个低级错误导致getActivity为空. 因为我在Fragment的构造函数中调用这个方法了..此时Acti ...

  7. My to do 12.25

    Merry Christmas 新的一年要来了,参加工作转眼也快半年了.回顾以往,多逢贵人.不忘初心,感慨良多.祝所有的朋友都能幸福,愿望都可以实现,日子越过越好~ Look Back 作为GH加入北 ...

  8. Android: 触屏fling/scroll/drag的区别及其详细过程

    Google了一下,终于搞清了touch screen下的几种操作模式(对应的是事件). 对于一个view, 常用的操作有点击(click)和长按(long press)二种.实际上,这些操作类型是A ...

  9. C#(二维数组/集合)

    一.二维数组int [,] array = new int[5,3];//有五个一维数组,每一个一维数组有3个元素 /打印出来一个“王”这个字string[,] wang = new string[, ...

  10. Centos下yum配置lnmp环境

    首先关闭SELINUX        vi /etc/selinux/config       #SELINUX=enforcing       #注释掉       #SELINUXTYPE=tar ...