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 ...
随机推荐
- LeetCode(238) Product of Array Except Self
题目 Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- linux中的部分宏
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- HDU 5489 Removed Interval DP 树状数组
题意: 给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度. 分析: 设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g ...
- 光学字符识别OCR-7语言模型
由于图像质量等原因,性能再好的识别模型,都会有识别错误的可能性,为了减少识别错误率,可以将识别问题跟统计语言模型结合起来,通过动态规划的方法给出最优的识别结果.这是改进OCR识别效果的重要方法之一. ...
- Java程序的结构和执行
目录 Java程序的结构 Java程序的执行 source code -- compiler -- class file -- JVM compiler JVM Java语法 数据类型 数据的存储 堆 ...
- [python] 求大神解释下 面向对象中方法和属性
面向对象中 类方法 实例方法 类属性 实例属性该如何理解呢?
- uiautomatorviewer打不开
uiautomatorviewer打不开: 因为之前我下载的jdk版本为10,后来将jdk版本改为8之后就可以打开了.
- [python IO学习篇]补充打开中文路径的文件
http://blog.csdn.net/mottolinux/article/details/525600621 关于Python编码的基本常识 在python里面 “明文”是unicode类型和s ...
- javascript基础 方法
两者的区别:定时器隔一段时间执行一次,延迟器只执行一次 在html中直接调用此方法会返回null
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...