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.
要求是一次过....看来基础还是有点差差的啊。
1.首先回顾一下链表的基本特性,从三点来分析下:
1> 存储分配方式,链表用一组任意的存储单元存放线性表的元素。
2>时间性能,查找O(n),插入和删除,找出某位置指针后,插入和删除时仅为O(1)。
3>空间性能,不需要分配存储空间,只要需要就可以malloc,所以也叫动态链表。
4> 插入和删除节点图示:

2.动态链表的创建(leedcode的链表是没有头结点),第一个形参参数是指针,要想改变指针的值,需要用引用或者指向指针的指针。
void CreateListHead(ListNode* &head, int n)
{
int j=;
head = (ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
head->val=v[j++];//v是用户输入的每个节点的val
ListNode* p=head;
for (int i=;i<n;++i)
{
ListNode* newNode;
newNode = (ListNode*)malloc(sizeof(ListNode));
p->next=newNode;
newNode->next=NULL;
newNode->val=v[j++];
p=p->next;
}
}
3.所有调试代码
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
int getLength(ListNode *head){
int length=;
while(head){
++length;
head=head->next;
}
return length;
}
void deleteNode(ListNode* &head,int loc)//第一个位置是1
{
if(loc==){
head=head->next;
}
else
{
ListNode* p=head;
int j=;
while (p->next&&j<loc-)
{
p=p->next;
++j;
}
p->next=p->next->next;
}
}
ListNode *removeNthFromEnd(ListNode *head, int n) {
if(head==NULL)
return NULL;
ListNode* res=head;//需要新建局部变量
int len=getLength(res);
int loc=len-n+;
deleteNode(res,loc);
return res;
}
};
vector<int> v;
int num;
void CreateListHead(ListNode* &head, int n)
{
int j=;
head = (ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
head->val=v[j++];
ListNode* p=head;
for (int i=;i<n;++i)
{
ListNode* newNode;
newNode = (ListNode*)malloc(sizeof(ListNode));
p->next=newNode;
newNode->next=NULL;
newNode->val=v[j++];
p=p->next;
}
}
int main()
{
freopen("C:\\Users\\Administrator\\Desktop\\test.txt","r",stdin);
cin>>num;
for (int i=;i<num;++i)
{
int temp;
cin>>temp;
v.push_back(temp);
}
ListNode* head=NULL;
CreateListHead(head,num);
Solution so;
ListNode* res=so.removeNthFromEnd(head,);
return ;
}
Remove Nth Node From End of List(链表,带测试代码)的更多相关文章
- remove Nth Node from linked list从链表中删除倒数第n个元素
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- Leetcode 19 Remove Nth Node From End of List 链表
删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- leetcode-algorithms-19 Remove Nth Node From End of List
leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- 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 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
随机推荐
- Python3学习了解日记
# 单行注释 ''' 多行注释 ''' """ 这个也是多行注释 """ ''' 声明变量 Python 中的变量不需要声明.每个变量在使用 ...
- SQL Server ALwayson 正在解析
原因:把主库切换到辅助副本以后,集群全部出现正在解析的情况,数据库显示“恢复挂起” 过程:把服务器重启,原以为正在解析会恢复正常.结果失败. 解决方法:出现“正在解析”的情况跟故障转移群集有关,进故障 ...
- GoF23种设计模式之行为型模式之观察者模式
一.概述 定义对象之间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.二.适用性1.当一个抽象模型有两个方面,其中一个方面依赖于另一方面的时 ...
- python之函数基础总结
定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi(name): print("Hello, %s, I', nobo ...
- console_init()分析
启动阶段初始化控制台流程分析, start_kernel console_init(); -->tty_ldisc_begin(); /* Setup the default TTY line ...
- How to install redis server on CentOS 7 / RHEL 7
在本教程中,我们将学习如何在CentOS 7 / RHEL 7上安装Redis服务器. redis的缩写是REmote DIctionary Server. 它是最流行的开源,高级键值缓存和存储之一. ...
- matlab 初级画图
matlab 初级画图 1.plot() plot(x,y) plots each vector pairs (x,y) 画图函数画出每个点 每组变量 plot (y) plots eac ...
- 九度oj 题目1084:整数拆分 清华大学2010年机试题目
题目描述: 一个整数总可以拆分为2的幂的和,例如:7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1+1+2+2 7=1+1+1+1+1+2 7=1+1+1+1+1+1+1总共有六种不 ...
- DS博客作业05——树
1.本周学习总结 1.1思维导图 1.2学习体会 学习:相比于之前的数据结构,树多了很多性质,相应的也多了很多计算题,不得不说,专有名词也是颇多.觉得树最独特的地方就是它的兄弟.孩子结点,用以组成了它 ...
- 【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理
题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的 ...