Remove Nth Node From End of List
2015-09-10 15:13
543 查看
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Note:
Given n will always be valid.
Try to do this in one pass.
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.
/** * 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) { int l=2; int i=2; ListNode *temp,*tt; temp=head->next; if(temp==NULL) { if(n==0) return head; else return NULL; } while(temp->next!=NULL) { temp=temp->next; l++; } if(n==l) { return head->next; free(head); } temp=head; while(temp->next!=NULL) { if(i==l-n+1) { tt=temp->next; temp->next=tt->next; free(tt); i++; continue; } temp=temp->next; i++; } return head; } };
相关文章推荐
- nodejs--express4.x session用法
- Leetcode52: Delete Node in a Linked List
- 【leetcode】Count Complete Tree Nodes -C++
- [环境]搭建Node+NPM+Grunt+Ruby开发环境
- LeetCode Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node
- CCNode解析
- 转-windows系统下简单nodejs安装及环境配置
- node.js querystring处理参数
- populate next right pointer in each node
- LeetCode -- Remove Nth Node from End of List
- Nodejs课堂笔记-第二课 package.json的作用
- 【前端学习笔记】2015-09-09~~~~nodejs中的require()和module.exports
- UVa 10223 - How many nodes ?
- 使用nvm进行node多版本管理
- GDataXMLNode配置
- chef client注册为node
- Node.js进程通信模块child_process