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. C# AOP实现

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting.Pro ...

  2. java中链表的数据(对象)位置交换

    用LinkedList类的set方法把引用 对象换了就行 ,如 import java.util.LinkedList; public class Tffdsafsdafsad { public st ...

  3. 如何查询centos、Debian服务器、查看系统内核版本,系统版本,32位还是64位

    查看centos内核的版本: 1)[root@localhost ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@build ...

  4. 30-懒省事的小明(priority_queue)

    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=55 懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   ...

  5. java类中final方法的作用

    不给子类复写这个方法.说明你已经知道这个方法提供的功能已经满足你要求,不需要进行扩展,并且也不允许任何从此类继承的类来覆写这个方法,但是继承仍然可以继承这个方法,也就是说可以直接使用 inline扩展 ...

  6. 线程同步synchronized,wait,notifyAll 测试示例

    https://www.cnblogs.com/LipeiNet/p/6475851.html 一  synchronized synchronized中文解释是同步,那么什么是同步呢,解释就是程序中 ...

  7. Linux下的strerror是否线程安全?

    下列是glibc-2.14中的源代码: 点击(此处)折叠或打开 char * strerror (errnum) int errnum; { char *ret = __strerror_r (err ...

  8. 使用JS完成首页定时弹出广告图片

    一.需求分析 在首页中的顶部做一个定时弹出广告图片. 二.技术分析 隐藏图片: display: none        定时操作: setInterval(“显示图片的函数”, 3000); 三.代 ...

  9. nagios+influxdb+grafana的监控数据可视化流程

    nagios介绍 nagios是一款开源监控的应用,可用于监控本地和远程主机的日志.资源.死活等等诸多功能.通过snmp协议和nrpe协议. nagios的配置文件是由nconf上进行配置,然后点击生 ...

  10. storm集群快速搭建

    sudo mkdir /export/serverssudo chmod -R 777 /exportmkdir /export/servers tar -zxvf apache-storm-1.0. ...