week9-leetcode #19-Remove-Nth-Node-From-End-of-List
来源:互联网 发布:如何做淘宝天猫客服 编辑:程序博客网 时间:2024/05/17 22:42
week9-leetcode #19-Remove-Nth-Node-From-End-of-List
链接:https://leetcode.com/problems/remove-nth-node-from-end-of-list/
Question
Given a linked list, remove the *n*th node from the end of list and return its head.
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.
Solution
time complecity:
space complecity:
class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { int number = 0; ListNode* p = head; while (p != NULL) { number++; p = p->next; } cout << "number: " << number << endl; int count = number-n; p = head; if (count == 0) { head = head->next; delete p; } else { while (count >= 0 && p != NULL) { cout << "count: " << count << endl; if (count == 1) { cout << "I am in " << endl; ListNode* q = p->next; p->next = p->next->next; delete q; break; } p = p->next; count--; } } return head; }};
思路:先对链表遍历一次可得到整个链表的大小,与倒数第几的数求的差值,这个差值就是正数第几个数,再遍历一次找到待删除的节点进行相应的操作。
阅读全文
0 0
- week9-leetcode #19-Remove-Nth-Node-From-End-of-List
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- Leetcode 19 Remove Nth Node From End of List
- LeetCode 19: Remove Nth Node From End of List
- [Leetcode 19, Easy] Remove Nth Node From End of List
- LeetCode(19) Remove Nth Node From End of List
- [LeetCode 19] Remove Nth Node From End of List Solutions
- leetcode 19 Remove Nth Node From End of List
- Leetcode【19】:Remove Nth Node From End of List
- [leetcode 19] Remove Nth Node From End of List
- [Leetcode] 19 - Remove Nth Node From End of List
- LeetCode | #19 Remove Nth Node From End of List
- leetcode.19----------Remove Nth Node From End of List
- [Leetcode 19, Easy] Remove Nth Node From End of List
- LeetCode 19 Remove Nth Node From End of List
- leetcode-19 Remove Nth Node From End of List
- leetCode #19 Remove Nth Node From End of List
- leetcode 19 Remove Nth Node From End of List
- python3.5《机器学习实战》学习笔记(五):决策树算法实战之预测隐形眼镜类型
- Uva247.Calling Circles(Floyd求连通性)
- 可变参数列表浅析
- c语言:错误C4473:“scanf_s”没有为格式字符串传递足够的参数
- 浅谈Android动画(二)
- week9-leetcode #19-Remove-Nth-Node-From-End-of-List
- 设计模式之JDK动态代理
- 软件测试--并非所有软件缺陷生来就是平等的
- SSM项目中存入数据库中文乱码的问题
- CSS伪类选择器(锚点伪类、行为伪类)
- EKF matlab
- 二叉树及其基本操作
- Install Visual Studio 6.0 on Windows 10
- POJ 2826 An Easy Problem?! <计算几何>