LeetCode(19) 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,
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;
}
};
LeetCode(19) Remove Nth Node From End of List的更多相关文章
- 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 ...
- 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 + ...
- 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 ...
- 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 ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(203) Remove LinkedList Elements
题目 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode(27)Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length ...
随机推荐
- 决斗(Headshot )
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; char ...
- Centos6.8 配置 Tomcat
1.安装Tomcat,安装之前必须先安装Java,先安装java 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index. ...
- UWP 动画
一:StoryBoard 一般翻译成演示图版或者故事板,就像电影中的情节串联板,它是一个动画时间线的容器. 二:动画的分类 简单动画:以Animation结尾,例如DoubleAnimat ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- DedeCMS全版本通杀SQL注入漏洞利用代码及工具
dedecms即织梦(PHP开源网站内容管理系统).织梦内容管理系统(DedeCms) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户最多的PHP类CMS系统,近日,网友 ...
- idea使用Git提交代码时忽略指定文件或文件夹
简述 使用idea编写代码并使用git作为版本控制器的时候,常常不需要提交配置文件以及一些其他不需要提交的文件,可以使用.ignore插件来在上传的时候忽略一些文件或文件夹. 安装 注意:安装完成之后 ...
- [ HEOI 2016 ] 树
\(\\\) Description 给出一颗树,开始只有 \(1\) 号节点有标记. \(\ C\ x\) 对 \(x\) 号节点打标记 \(\ Q\ x\) 查询 \(x\) 号节点深度最深的有标 ...
- 【学习笔记】OSG中相机参数的更改
#pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...
- 如何正确理解和使用 Activity的4种启动模式
关于Activity启动模式的文章已经很多,但有的文章写得过于简单,有的则过于注重细节,本文想取一个折中,只关注最重要和最常用的概念,原理和使用方法,便于读者正确应用. Activity的启动模式有4 ...
- 第8章 应用协议 图解TCP/IP 详解
第8章 应用协议 图解TCP/IP 详解 8.1 应用层协议概要 应用层协议的定义 TCP和IP等下层协议是不依赖上层应用类型.实用性非常广的协议.而应用协议则是为了实现某种应用而设计和创造的协议. ...