leetcode题解-206. Reverse Linked List
来源:互联网 发布:测绘数据泄密案例 编辑:程序博客网 时间:2024/05/16 11:35
题目:
Reverse a singly linked list.click to show more hints.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?
本题就是将链表翻转,按照提示可以根据循环和递归两种方式进行,比较简单不做过多解释,直接上代码:
//循环法 public ListNode reverseList(ListNode head) { ListNode cur=head, next=head, pre=null; while(cur != null){ next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; }
//递归法 public static ListNode reverseList1(ListNode head){ ListNode cur = head; return dfs(cur, null); } public static ListNode dfs(ListNode cur, ListNode pre){ if(cur == null) return pre; ListNode next = cur.next; cur.next = pre; pre = cur; cur = next; return dfs(cur, pre); }
阅读全文
0 0
- Leetcode题解 206. Reverse Linked List
- leetcode题解-206. Reverse Linked List
- LeetCode题解:Reverse Linked List
- LeetCode题解:Reverse Linked List
- LeetCode题解:Reverse Linked List
- LeetCode 206. Reverse Linked List 题解(C++)
- LeetCode题解(Week 3):206. Reverse Linked List
- LeetCode题解: Reverse Linked List II
- Reverse Linked List II|leetcode题解
- LeetCode 题解(141): Reverse Linked List
- LeetCode题解——Reverse Linked List
- LeetCode题解:Reverse Linked List II
- LeetCode题解-92-Reverse Linked List II
- LeetCode题解-206-Reverse Linked List Ⅰ
- leetcode题解-92. Reverse Linked List II
- leetcode题解-92. Reverse Linked List II
- 【LeetCode】206.reverse linked list
- [leetcode] 206.Reverse Linked List
- Blog项目 —上集
- 创建新表时的导出表sql
- 关于hibernate一级缓存
- Linux ubuntu 安装MySql
- Spring-Boot初学之配置文件application.properties(数据源配置)
- leetcode题解-206. Reverse Linked List
- 将博客搬至CSDN
- Linux下的静态库与动态库
- 一分钟了解“Matlab实现max-mean滤波”
- kaldi安装编译
- html嵌套规则
- 类之间相同字段值传递工具(反射机制)
- Android自定义View实现滴滴验证码输入框效果
- java经典书籍推荐