LeetCode_206 Reverse Linked List
2015-08-03 11:34
141 查看
Reverse a singly linked list.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
pre始终是首元结点的值,每次对pnext重新赋值
递归
非递归
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
pre始终是首元结点的值,每次对pnext重新赋值
递归
public ListNode reverseList(ListNode head) { if(head == null ) return null; if(head.next == null) return head; ListNode p = head.next; ListNode q = reverseList(p); head.next = null; p.next = head; return q; }
非递归
public ListNode reverseList1(ListNode head) { if(head == null ) return null; if(head.next == null) return head; ListNode pre = head; ListNode p = head.next; head.next = null; ListNode tmp ; while(p!=null){ tmp = p.next; p.next = pre; pre = p; p = tmp; } return pre; }
相关文章推荐
- codeforces 26B Regular Bracket Sequence(暴力)
- POJ 2240 Arbitrage Bellman_ford
- QtSQL SERVER连接数据库
- 使用dom4j把XML文件解析成JavaBean
- [LeetCode]230.Kth Smallest Element in a BST
- Linux下清除DNS缓存
- 转--O2O刷单“黑市”折射下的泡沫#神作#
- POJ 1986 Distance Queries
- vs2013下openSIFT的编译调试
- MongoDB Windows环境安装及配置
- WebStorm 注册码
- Android开发中,一些相见恨晚的方法、类或接口
- android中使用PrinterShare实现蓝牙打印功能
- 在Ext的GridPanel中添加鼠标悬停的效果
- 常量指针和指针常量的区别详解
- 求相交圆面积
- js相对路径相关(比如:js中的路径依赖导入该js文件的路径)
- STM32学习笔记(四) RCC外设的学习和理解
- vmware启动虚拟机报“内部错误”的解决方法
- 下载地址: