Level:

  Medium

题目描述:

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.

思路分析:

  题目要求删除链表的倒数第n个节点,首先我们需要判断n是否在有效范围,然后我们计算出链表的长度,求倒数第n个,就是求正数第len-n个。

代码:

public class ListNode{
int val;
ListNode next;
public ListNode(int x ){
val=x;
}
}
public class Solution{
public ListNode removeNthFromEnd(ListNode head,int n){
if(head==null)
return null;
int len=0;
ListNode pNode=head;
while(pNode!=null){
len++;
pNode=pNode.next;
}
if(n>len)
return null;
if(len==1&&len==n)
return null;
if(len==n)
return head.next;
ListNode pNode2=head;
for(int i=0;i<len-n-1;i++){
pNode2=pNode2.next;
}
pNode2.next=pNode2.next.next;
return head;
}
}

25.Remove Nth Node From End of List(删除链表的倒数第n个节点)的更多相关文章

  1. lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

    题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点.  样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...

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

  3. 019 Remove Nth Node From End of List 删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点并返回头结点.例如,给定一个链表: 1->2->3->4->5, 并且 n = 2.当删除了倒数第二个节点后链表变成了 1->2 ...

  4. 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)

    这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...

  5. Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...

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

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

  7. 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)

    题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了   2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...

  8. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

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

随机推荐

  1. MySQL的FORMAT函数用法规则

    1.FORMAT函数在mysql中是数据内容格式化的,格式化后得到结果:###,###,#####. ,); 输出结果: ,000.00 2.可以格式化数据为整数或者浮点数. ); 输出结果: 100 ...

  2. 批量插入数据insert into select示例

    //增加 $addSql =" insert into hxqc_auth_group_limits(group_id,company_id)"; foreach ($add_da ...

  3. 【bzoj2144】跳跳棋

    2144: 跳跳棋 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 492  Solved: 244[Submit][Status][Discuss] ...

  4. selenium3加载浏览器

    浏览器禁用更新: 因为selenium对浏览器的支持是有限制的.当浏览器更新到最新版本时,需要下载支持最新版本的插件.有时候selenium还没有更新到支持最新版本的插件,但本地已经更新到最新版本了. ...

  5. 使用Java创建XML数据

    ------------siwuxie095                         工程名:TestCreateXML 包名:com.siwuxie095.xml 类名:CreateXML. ...

  6. iis8不支持 aspnet_regiis.exe -iru 命令的解决办法

    服务器版的限制,我看你给的提示说也可以使用 dism.exe 命令行. C:\> DISM /Online /Enable-Feature /FeatureName:WCF-HTTP-Activ ...

  7. Docker学习笔记_删除某个镜像

    实验:删除某个镜像 sudo docker rmi [Image ID] 1.查看镜像的ID                  sudo docker images 2.删除镜像            ...

  8. HYSBZ 1036 树的统计Count (水题树链剖分)

    题意:中文题. 析:就是直接维护一个最大值和一个和,用线段树维护即可,这个题很简单,但是我卡了一晚上,就是在定位的时候,位置直接反过来了,但是样例全过了...真是... 代码如下: #pragma c ...

  9. (转)第一次发博客-说说我的B/S开发框架(asp.net mvc + web api + easyui)

    原文地址:http://www.cnblogs.com/xqin/archive/2013/05/29/3105291.html 前言 这些年一直在.net下做企业web系统开发,前前后后经历的不同的 ...

  10. MongoDB整理笔记の导入导出

    一.导入 1.导入json数据 我们先将表user删除掉,以便演示效果: > db.user.drop(); true > show collections; system.indexes ...