[LeetCode]Remove N-th node from end of list
来源:互联网 发布:流星网络电视vip破解版 编辑:程序博客网 时间:2024/05/18 20:09
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.
class Solution {public: ListNode *removeNthFromEnd(ListNode *head, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(head==NULL) return NULL; ListNode *n1=head; ListNode *n2=head; ListNode *tmp; for(int i=0;i<n;i++) { if(n2==NULL) //size of lists < n return head; n2=n2->next; } if(n2==NULL) { //remove header tmp=head; head=n1->next; delete tmp; return head; } while(n2->next!=NULL) { n1=n1->next; n2=n2->next; } //remove n1->next; tmp=n1->next; n1->next=n1->next->next; delete tmp; return head; }};
- [LeetCode]Remove N-th node from end of list
- [LeetCode]Remove N-th Node from the end of the 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
- Remove Nth Node From End of List 链表删除倒数第N个元素@LeetCode
- LeetCode Remove Nth Node From End of List 删除倒数第n个元素
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- LeetCode-19 Remove Nth Node From End of List(移除尾部第N个节点)
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- Leetcode Remove Nth Node From End of List 删除链表倒数第n个元素
- [LeetCode-19] Remove Nth Node From End of List(删除倒数第N个节点)
- leetcode 19. Remove Nth Node From End of List 单向链表删除第n个数
- 【LeetCode】Remove Nth Node From End of List 删除链表中倒数第n个节点- Easy +
- 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
- QA System
- Windows下安装MySQL解压版
- 长方体类
- Sikuli
- sar usage
- [LeetCode]Remove N-th node from end of list
- 台湾电脑术语与大陆术语对比表
- shader 编程工具:NVIDIA FX Composer和Render Monkey
- IOS DES加解密(带汉字的NSString的加解密)
- 第四周任务2——三角形类
- iOS使用NSURLConnection发送同步和异步HTTP Request
- 第四周项目1:三角形类
- Ubuntu安装QQ2012
- padding和margin