题目

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

For 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.

Try to do this in one pass.

分析

给定一个链表头结点与整数n,要求删除链表中倒数第n个结点,返回链表头结点。

题目不难,主要是必须考虑周全;

  • head==NULL || n==0,则直接返回head
  • 链表中结点总数count < n,则直接返回head
  • 链表中结点总数count == n,则head=head->next,返回head
  • 删除中间或尾结点,即删除正序第count-n+1 个结点,将倒序改为正序处理

AC代码

/**
* 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) {
if (head == NULL || n == 0 )
return head; //计算链表中的结点个数
int count = 0;
ListNode *p = head;
while (p)
{
count++;
p = p->next;
} //链表中的结点个数小于要删除的倒数第n个
if (count < n)
{
return head;
}
else if (count == n)
{
ListNode *tem = head;
head = head->next;
delete tem;
}
else{
ListNode *p = head, *q = p->next;
int i = 1;
while (i < (count - n) && q->next!=NULL)
{
p = p->next;
q = q->next;
i++;
}
p->next = q->next;
delete q;
}
return head; }
};

GitHub测试程序源码

LeetCode(19) Remove Nth Node From End of List的更多相关文章

  1. leetcode第19题--Remove Nth Node From End of List

    Problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...

  2. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  3. LeetCode之“链表”:Remove Nth Node From End of List

    题目链接 题目要求: Given a linked list, remove the nth node from the end of list and return its head. For ex ...

  4. LeetCode 笔记系列四 Remove Nth Node From End of List

    题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Gi ...

  5. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. LeetCode(203) Remove LinkedList Elements

    题目 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...

  7. LeetCode(82)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  8. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  9. LeetCode(27)Remove Element

    题目 Given an array and a value, remove all instances of that value in place and return the new length ...

随机推荐

  1. 我的spring-boot开发环境

    我的spring-boot开发环境,目的方便我快速搭建开发环境,同时可以最佳实践.使用spring-boot 2.1.x. 代码地址:GitHub my-springboot-examples 目的是 ...

  2. PHPmail 亲测可用

    2017年5月8日9:10:47 1.在模块的配置文件中加入下里面代码,账号最好用126邮箱'THINK_EMAIL' => array( 'SMTP_HOST' => 'smtp.163 ...

  3. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

  4. josephus 问题的算法(转载)

    Josephus 问题: 一群小孩围成一个圈,任意假定一个数 m,从第一个小孩起,顺时针方向数,每数到第 m 个小孩时,该小孩便离开.小孩不断离开,圈子不断缩小,最后剩下的一个小孩便是胜利者.究竟胜利 ...

  5. 166 Fraction to Recurring Decimal 分数到小数

    给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如,    给出 分子 = 1, 分母 = 2,返回 "0.5".  ...

  6. linux小白成长之路13————用U盘安装linux服务器

    [内容指引] 制作CentOS安装引导盘: 安装CentOS: 相关设置: 一.制作CentOS安装引导盘 1.下载安装镜像文件 从官网下载iso文件: 网址:https://www.centos.o ...

  7. 关于获取计算机唯一ID问题

    1:CPU序列号,并不是每颗CPU都有一个唯一的序列号,CPU试每种型号一个序列号,其实可以认为是CPU型号号码.PIII以前的计算机没有ID,而且AMD的CPU也没有ID. 创建一个虚拟机,他会重新 ...

  8. iOS9 开发新特性 Spotlight使用

    1.Spotloight是什么? Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotl ...

  9. B树、B+树、红黑树、AVL树

    定义及概念 B树 二叉树的深度较大,在查找时会造成I/O读写频繁,查询效率低下,所以引入了多叉树的结构,也就是B树.阶为M的B树具有以下性质: 1.根节点在不为叶子节点的情况下儿子数为 2 ~ M2. ...

  10. Oracle EXPDP and IMPDP

    一.特点 • 可通过 DBMS_DATAPUMP 调用 • 可提供以下工具: – expdp – impdp – 基于 Web 的界面 • 提供四种数据移动方法: – 数据文件复制 – 直接路径 – ...