题目

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. iOS 监测电话呼入

    1.首先引入CoreTelephony框架,代码里: @import CoreTelephony; 项目设置里: 2.定义属性,建立强引用: @property (nonatomic, strong) ...

  2. Secrets CodeForces - 333A

    Secrets CodeForces - 333A 题意:这个世界上只有这样面值的硬币:1,3,9,27,81,...有一个商人,某一天遇到了一个顾客,他购买了价值n的商品,发现用自己的硬币无法付给商 ...

  3. Java对象的内存布局以及对象的访问定位

    一 Java对象的内存布局 在HotSpot虚拟机中,对象在内存中的布局分为3个区域 对象头(Header) Mark Word(在32bit和64bit虚拟机上长度分别为32bit和64bit)存储 ...

  4. 增强的for循环

  5. ISCSI存储

    slave-147作为服务端 需要安装的软件 [root@slave-147 ~]# yum install -y scsi-target-utils slave-148和slave-149作为客户端 ...

  6. P1789 【Mc生存】插火把

    题目背景 初一党应该都知道...... 题目描述 话说有一天linyorson在Mc开了一个超平坦世界,他把这个世界看成一个n*n的方阵,现在他有m个火把和k个萤石,分别放在x1,y1...xm,ym ...

  7. ubuntu中mysql安装失败

    在ubuntu中mysql安装失败后,卸载重新安装还是安装失败,之后找了资料说是卸载的不干净,然后进行下面操作,重新安装成功. 解决办法如下: sudo rm /var/lib/mysql/ -Rsu ...

  8. Java GUI简介

    Java有2个GUI库:AWT.Swing. AWT是SUN最早提供的GUI库,依赖本地平台,界面不好看,功能有限.之后推出了Swing,Swing并没有完全替代AWT,而是建立在AWT基础上的.Sw ...

  9. re匹配语法-match、search和findall

    1.re.match() 匹配第一个值 列表里的值可以有多个范围,有一个符合就可以. match只匹配第一个值,所以列表里的范围是第一个值得取值范围.如果第一个值被设定好且存在,那么列表的取值范围变为 ...

  10. 无法登录phpmyadmin,报1130错误

    分析过程及解决方案: mysql的1130错误是远程连接的用户无远程权限问题导致.解决方案:在本机登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”loc ...