Leetcode 206
来源:互联网 发布:淘宝白底图拍摄技巧 编辑:程序博客网 时间:2024/04/24 07:27
Reverse a singly linked list.
Recursion:
<pre name="code" class="cpp">/*** Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseList(ListNode* head) {//反转,不是遍历,直接反转内容不是反转输出 if(head==NULL) return NULL; if(head->next==NULL) return head; ListNode *p=head->next; ListNode *n=reverseList(p); head->next=NULL; p->next=head; return n;//return n把n最后一个链表值作为头返回 }};
Non-recursion:
<pre name="code" class="cpp">/*** Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseList(ListNode* head) {//反转,不是遍历,直接反转内容不是反转输出 if(head==NULL || head->next==NULL) return head;//递归的效率低,所以不要用递归 ListNode *pre=head; ListNode *p=pre->next; pre->next=NULL; ListNode *t; while(p!=NULL) { t = p->next; p->next=pre; pre = p; p = t; } return pre;//最后一个pre是最后一个值 }};
0 0
- Leetcode 206
- LEETCODE 206
- Leetcode 206
- leetcode 206
- LeetCode #206
- leetcode 237和leetcode 206
- <LeetCode><Easy>206 ReverseLinkedList
- LeetCode 206 单链表逆置
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- 去除tableView多余的分割线
- Struts2框架中HTTP Status 404 There is no Action mapped for namespace / and action name
- FreeBSD 安裝記錄
- ubuntu服务器添加用户实例
- yjuyt
- Leetcode 206
- maven安装
- scikit-image 图像处理
- 用记事本写第一个Servlet程序
- 原生js实现表单的正则验证,所有验证都通过后提交按钮才可用
- iOSCoreAnimation动画系列教程(一):CABasicAnimation
- 微软Hololens应用及开发——HoloLens介绍篇(三)应用界面
- 从IRQ到IRQL(APIC版)
- acitvity的runOnUiThread方法