题目

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. 最短路之SPFA

    解决存在<<<负环>>>的图的单源最短路径: 判断有无负环: 如果某个点进入队列的次数超过N次则存在负环(SPFA无法处理带负环的图) 这里,只介绍用bfs(深搜) ...

  2. Centos 6.8 配置Lvs

    LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. 宗旨: 使 ...

  3. [转]Adaboost 算法的原理与推导

    看了很多篇解释关于Adaboost的博文,觉得这篇写得很好,因此转载来自己的博客中,以便学习和查阅. 原文地址:<Adaboost 算法的原理与推导>,主要内容可分为三块,Adaboost ...

  4. word2vec的Java源码【转】

    一.核心代码 word2vec.java package com.ansj.vec; import java.io.*; import java.lang.reflect.Array; import ...

  5. 生成HTML表格的后台模板代码

    有时候,我们需要在后台拼接生成前端的html表格,一般的做法就是各种string.StringBuilder的拼接(例子省略...),这样的话如果表头不同就没法做到代码的重用,增加代码的冗余,下面我分 ...

  6. 上交oj1219 重要的逆序数对

    题意: https://acm.sjtu.edu.cn/OnlineJudge/problem/1219 思路: 在经典的归并排序求逆序数对算法基础上稍作修改. 实现: #include <io ...

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

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

  8. 基于 python 的接口测试框架

    项目背景 公司内部的软件采用B/S架构,管理实验室数据,实现数据的存储和分析统计.大部分是数据的增删改查,由于还在开发阶段,所以UI界面的变化非常快,之前尝试过用python+selenium进行UI ...

  9. iptables规则的关系

    iptables规则的关系,是自上而下进行过虑的.所以添加规则时,要通过文件进行添加,这样的话,可以控制其顺序. A机器: [root@www ~]# netstat -an | grep 6100 ...

  10. zc.buildout构建项目时报错‘AttributeError: '_NamespacePath' object has no attribute 'sort'’

    在使用zc.buildout构建项目时如果碰到‘AttributeError: '_NamespacePath' object has no attribute 'sort'’报错: An inter ...