基本思路

  1. 定义两个指示指针a b
  2. 让a先行移动n+1个位置
  3. 若a指向了NULL的位置,则删除的是头节点(由于走过了n+1个节点刚好指在尾部的NULL上)
  4. 否则让b与a一起移动直至a->next,即a的下一个节点为NULL,则此时b的下一个节点为要删除的节点
  5. 删除下一个节点

代码实现

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* a = head;
ListNode *b =head;
int c = 1;
//令a指向第n+1个节点
while(c!=n+1){
a = a->next;
c++;
}
if(a == NULL){
ListNode * t = head;
head = head->next;
delete t;
return head;
} //a b 同时向前走
while(a->next != NULL){
a = a->next;
b = b->next;
} ListNode * t = b->next;
b->next = b->next->next;
delete t;
return head;
}
};

LeetCode 删除链表倒数第N个节点的更多相关文章

  1. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  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. 删除链表倒数第n个节点

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

  4. 19。删除链表倒数第N个节点

    class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next# 这道题还是很简单的,我们只 ...

  5. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  6. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  7. 删除单链表倒数第n个节点

    基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...

  8. leetcode 19.删除链表的第n个节点

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

  9. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

随机推荐

  1. java消息中间件 RocketMQ Linux安装与运行

    阿里巴巴宣布捐赠RocketMQ到Apache软件基金会孵化项目,最近闲下来便去部署了一个试验版本玩玩. 至于RockeMQ是什么,原理架构什么的这里就不赘述了,这里只记录安装过程. 一.系统环境 s ...

  2. Form表单元素

    Form表单元素 action method input: name value type: text password button radio checkbox file submit reset ...

  3. Windows下COCOS2D-X开发环境配置

    1. 下载Android SDK: http://developer.android.com/sdk/index.html ,解压到E:\ADT 目录下 2. 下载NDK: http://develo ...

  4. dpkg 打包root权限app

    dpkg 安装Macports 下载对应系统的Macports安装时间会比较久,安装完毕后放在了/opt/local/bin 目录下 安装dpkg 打开终端,输入 sudo port -f insta ...

  5. matlab练习程序(Arnold图像置乱)

    自从上次写了Hilbert图像置乱之后,就对图像置乱研究了一下,发现这里面也是有很多置乱算法的. Arnold也算一种比较主要的置乱算法,算法由以下变换公式产生: 这里a和b是参数,n是迭代次数,N是 ...

  6. nginx http强制跳转https

    通过nginx的rewrite 进行301永久重定向,参考如下配置即可. server { listen  192.168.1.111:80; server_name test.com; rewrit ...

  7. 《浪潮之巅》(第2版):精彩的IT商战史

    2011年看过第一版.以为看新版会跳过大部分看过的内容,结果发现还是从头到尾看了一遍,2011年看过的内容已经记不太确切了:) 另外IT的历史太精彩了,许多故事都知道,再看还是挺有意思.当然作者的文字 ...

  8. ccsu小助手

    CCSU小助手 队名:瓜队 组员:钟文兴.周畅.吉刘磊.唐仲勋 宣言:We are a team at any time! 团队项目描述: 内容:“生活在长大”: 目标:为了方便对学校不了解的学生能够 ...

  9. Ubuntu 配置java环境变量

    1.使用如下命令,打开/etc/profile: $sudo vi /etc/profile 2.进入编辑模式,在末尾添加: #developer enviroment, add by myself ...

  10. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...