题目:

删除链表中倒数第n个节点

给定一个链表,删除链表中倒数第n个节点,返回链表的头节点。

 样例

给出链表1->2->3->4->5->null和 n = 2.

删除倒数第二个节点之后,这个链表将变成1->2->3->5->null.

注意 链表中的节点个数大于等于n

解题:

要删除倒数第n个节点,我们要找到其前面一个节点,也就是倒数第n+1的节点,找到这个节点就可以进行删除。和上题的思想很类似,

定义两个指针,p和cur,cur指针向前走,走了n+1步后,p指针开始走,当cur走到结束时候的,p指向倒数n+1的节点

程序中注释部分要注意,当删除的是第一个节点时候,找不到其前一个节点,通过n==1的时候做判断

这里要注意在只有一个节点,也让你删除这个节点,好像只有单独考虑的,题目的节点数大于等于n,没有考虑节点数小于n的情况

Java程序:

/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: The head of linked list.
*/
ListNode removeNthFromEnd(ListNode head, int n) {
// write your code here
ListNode p = new ListNode(0);
p.next = head;
ListNode cur = new ListNode(0);
cur.next = head;
cur = cur.next;
if(n==1 && head.next==null)
return null;
while(cur!=null){
if(n>0){
cur = cur.next;
n--;
}else if(n==0){
cur = cur.next;
head = head.next; }
if(cur.next==null){// cur运行到最后一个节点
if(n==1)//说明head指针没有移动,结合cur可知道,删除的是第一个节点,
return p.next.next;
if(head.next.next==null)
head.next = null;//删除的是最后一个节点
else
head.next = head.next.next;//删除的是中间部分的节点
break;
} }
return p.next;
}
}

总耗时: 2934 ms
Python程序:

"""
Definition of ListNode
class ListNode(object): def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of linked list.
@param n: An integer.
@return: The head of linked list.
"""
def removeNthFromEnd(self, head, n):
# write your code here
p = ListNode(0)
p.next = head
cur = ListNode(0)
cur.next = head
cur = cur.next
if n==1 and head.next==None:
return None
while cur!=None:
if n>0:
cur = cur.next
n-=1
elif n==0:
cur = cur.next
head = head.next
if cur.next==None:
if n==1:
return p.next.next
if head.next.next==None:
head.next=None
else:
head.next = head.next.next
break
return p.next

总耗时: 519 ms

lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点的更多相关文章

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

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

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

  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. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

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

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

  9. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

随机推荐

  1. JQuery之proxy实现绑定代理

    在javascript中,this指代的对象时常会变化,这会造成程序,混乱,一般做法就是先将this保存在一个变量中,就不怕她变了,我们先看一个小例子 var A = function(){ this ...

  2. JQuery插件开发 - 模板

    (function($) { $.fn.PluginName = function(options) { // 创建一个默认设置对象 var defaults = { key : "Defa ...

  3. LINQ to XML(1)

    LINQ to XML可以两种方式和XML配合使用.第一种方式是作为简化的XML操作API,第二种方式是使用LINQ查询工具.下面我使用的是第二种方式. 主要内容:用LINQ查询语句对XML文件里的数 ...

  4. centos问题集锦

    一. 为什么新装的centos系统无法使用xshell,putty等工具连接? 原因:sshd服务没有启动. 解决: 1)使用命令rpm -qa | grep ssh查看是否已经安装了ssh 2)使用 ...

  5. 关于华为C8812救砖教程

    问题:华为C8812刷机后,开机显示:---------------------------------------------------------------- Image signature ...

  6. DataSnap如何监控Tcp/IP客户端的连接情况

    一个实例,如果客户端是TCP/IP是短连接的情况就没有必要了. 一.GlobVar.pas单元,定义应用系统全局数据类型及变量: unit GlobVar; interface uses System ...

  7. How to: Enable and Disable an Action Pane Button on a List Page [AX 2012]

    Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynami ...

  8. 你必须知道的.NET

    作者博客地址:http://www.cnblogs.com/anytao/archive/2008/04/09/anytao_insidenet_center.html 第1章 OO大智慧 1.1对象 ...

  9. Struct是干什么的

    对于结构(Struct)这一看起来比较特殊的东西(用的比较少,只好用东西来形容了),真心用得少,只有在被问起的时候,才会想起,看看它到底是什么吧. 先给一个链接:http://www.cnblogs. ...

  10. (转)Android Support Percent百分比布局

    一.概述 周末游戏打得过猛,于是周天熬夜码代码,周一早上浑浑噩噩的发现 android-percent-support-lib-sample(https://github.com/JulienGeno ...