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. 判定元素是否刚插入到DOM树

    上接<这篇博文>,其应用于avalon的if绑定.如果一个节点还没有插入DOM树,那么avalon将延时对它进行扫描渲染,直到它再次插入到DOM树为止.由于CSS3 keyframe动画的 ...

  2. Oracle与SQL Server实现表数据同步

    将SQLServer2008中的某些表同步到Oracle数据库中,不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现. 实例1:SQLServer2008有一个表employ_ ...

  3. myeclipse,eclipse设置编码格式的4种情况,以及遇见部分问题的解决办法。

    (1).设置myeclipse工作空间的编码格式,作用范围最大 window-->preference-->general-->workspace-->text file en ...

  4. Linux常用基本命令 1

    useradd 创建用户. password 修改密码. date 查看时间 man date 帮助文档.f往后翻 b往前翻 q退出.软修改 man hwclock 修改硬件时钟, cal 查看日历 ...

  5. win7 下安装mysql 整理

    1.去官网下载mysql-5.6.13-winx64.zip包.地址: http://dev.mysql.com/downloads/mysql/5.6.html 2,把安装包解压到自己指定的目录,我 ...

  6. 2-JRE System Libraty [eclipse-mars](unbound)

  7. EXE DLL等可执行程序添加版本号版权等信息

    在使用Microsoft Visual Studio开发工具等编写的exe或者dll等可执行文件时,我们往往需要对这些可执行文件添加版本号,公司,版权等信息. 1. 在我们需要添加各种信息的项目工程中 ...

  8. IFM设备 Linux方面资料

    Github: https://github.com/lovepark/ifm3d

  9. c# 实现点击下载功能

    转自百度知道 private void DownLoad(string strName, string strPath) { string fileName = strName;//客户端保存的文件名 ...

  10. Ubuntu的Unable to locate package无法更新源问题解决方案

    https://blog.csdn.net/long19910605/article/details/47017889/ 问题: 更新源时提示不能联网(does the network require ...