leetcode之链表-单链表反转-C++
2015-07-21 10:53
477 查看
ReverseLinked List
A linked list can be reversed eitheriteratively or recursively. Could you implement both?
迭代版本:
1. /** 2. * Definition for singly-linked list. 3. * struct ListNode { 4. * int val; 5. * ListNode *next; 6. * ListNode(int x) : val(x), next(NULL) {} 7. * }; 8. */ 9. class Solution { 10. public: 11. ListNode* reverseList(ListNode* head) { 12. ListNode *p; 13. ListNode *q; 14. ListNode *r; 15. 16. if(head == NULL || head->next == NULL) 17. { 18. return head; 19. } 20. else 21. { 22. p = head; 23. q = head->next; 24. head->next = NULL; 25. 26. while(q) 27. { 28. r = q->next; 29. q->next = p; 30. p = q; 31. q = r; 32. } 33. head = p; 34. } 35. return head; 36. } 37. };
相关文章推荐
- c++ 计蒜客第11题移除数组重复元素
- 黑马程序员_C语言基础_分支结构(if……else,switch)
- Online Judge System For SzNOI 题库 语法百题 C++ d003
- C++ 操作符重载
- C++中string和vector用法总结
- Online Judge System For SzNOI 题库 语法百题 C++ d002
- c++ map 的基本操作
- [C++设计模式]template 模板方法模式
- Online Judge System For SzNOI 题库 语法百题 C++ d001
- 标准的window API 便利文件夹 c++
- C/c++几个预定义的宏:__DATE__,__TIME__,__FILE__,__LINE__
- 浅析pragma once与include guard
- C语言之字符串模糊查询方法的实现
- c++运算符重载之数据间的类型转换
- (转)C++文件的批处理——批量文件增删改查
- MFC中C++类如何封装API
- C++学习笔记碎屑--结构体与构造函数
- C++中static关键字的总结
- 黑马程序员_C语言基础_关键字、printf函数、scanf函数
- VC++ 多线程模拟鼠标点击