leetcode19
2015-10-06 22:16
204 查看
LeetCode19 --------Remove Nth Node From End of List
链表操作。
我的思路是:
1.先计算出链表的长度len,再根据其求出要删除的元素的正序。
2.考虑头结点的操作。
代码:
链表操作。
我的思路是:
1.先计算出链表的长度len,再根据其求出要删除的元素的正序。
2.考虑头结点的操作。
代码:
class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode*ptr1 = head; int count =1; while(ptr1->next != NULL) { ptr1 = ptr1->next; count ++; } n= count - n; if(n == 0) { ptr1=head; head=head->next; delete ptr1; return head; } ptr1 = head; ListNode*ptr2=head; while(n --) { ptr1= ptr1->next; } while(ptr2!=ptr1&&ptr2->next != ptr1) { ptr2=ptr2->next; } ptr2->next = ptr1->next; delete ptr1; return head; } };
相关文章推荐
- 关于ActionBar
- 【解题报告】CF Round #320 (Div. 2)
- 作业不会写
- 相似图片搜索的原理(二)(转)
- adb命令查看报名和查看手机分辨率
- HTML页面跳转的5种方式
- 相似图片搜索的原理(转)
- 项目框架设计
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- 菜鸟学数据库(三)——存储过程
- C++ 智能指针详解
- (转)菜鸟学数据库(三)——存储过程
- 【数据库】MFC ODBC(三)
- leetcode18
- maven 以及maven 插件 下载安装
- kernel中如何读取内存的大小和地址
- 文库的构建及测序(未完待续)
- Android横竖屏相关
- Delphi中的结构体与类
- 最快速度找到内存泄漏