[LeetCode]Remove Nth Node From End of List
来源:互联网 发布:哪个论坛源码好移动版 编辑:程序博客网 时间:2024/06/16 00:11
解题思路:
就提一点。一般要删除一个node,我们最好知道它前一个 preNode。这就存在一个问题,如果要删除head,用之前的方法肯定出错。
办法:在head前加一个 新的空node。
边界条件:head不为空
前条件:length, front->next = head
不变式:遍历list到想要删除的prenode
/** * 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) return NULL; ListNode* front = new ListNode(0); front->next = head; ListNode* cur = front; int length = 0; while(cur != NULL){ length ++; cur = cur->next; } int deleteIndex = length - n; cur = front; while ( --deleteIndex ){ cur = cur->next; } ListNode* temp = cur->next; cur->next = temp->next; free(temp); return front->next; }};
0 0
- [LeetCode]Remove Nth Node From End of List
- LeetCode[Linked List]: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List leetcode list
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode 40: Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- Leetcode 19 Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- [LeetCode]Remove Nth Node From End of List
- LeetCode-Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- LeetCode 19: Remove Nth Node From End of List
- LeetCode - Remove Nth Node From End of List
- LeetCode | Remove Nth Node From End of List
- poj 2255
- 反射
- 嵌入式Linux之我行——S3C2440上LCD驱动(FrameBuffer)实例开发讲解(二)
- C和C++中的const区别
- SQL注入原理以及如何避免注入
- [LeetCode]Remove Nth Node From End of List
- iOS8 Core Location (授权。。。室内定位。。。访问监控。。。)
- 老工程师对研发工作的感悟
- 探索 ConcurrentHashMap 高并发性的实现机制
- W3C理解
- ios NSScanner 扫描字符串获取相应的需要的字符串
- PopupWindow用法
- 最近学习Python 小小总结
- DB2与Oracle 使用的区别