92. Reverse Linked List II
2016-05-09 10:38
429 查看
Reverse Linked List II
代码
public class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { if(head == null){ return null; } ListNode dummy = new ListNode(0); dummy.next = head; ListNode pre = dummy; for(int i = 0; i<m-1; i++) pre = pre.next; ListNode start = pre.next; // a pointer to the beginning of a sub-list that will be reversed ListNode then = start.next; // a pointer to a node that will be reversed // 1 - 2 -3 - 4 - 5 ; m=2; n =4 ---> pre = 1, start = 2, then = 3 // dummy-> 1 -> 2 -> 3 -> 4 -> 5 for(int i=0; i<n-m; i++) { start.next = then.next; then.next = pre.next; pre.next = then; then = start.next; } // first reversing : dummy->1 - 3 - 2 - 4 - 5; pre = 1, start = 2, then = 4 // second reversing: dummy->1 - 4 - 3 - 2 - 5; pre = 1, start = 2, then = 5 (finish) return dummy.next; } }
相关文章推荐
- MSSql Server 自定义导出
- Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file: /tmp/hsperfdata_hadoop/44512
- -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
- 域名服务管理信息
- java对cookie的操作
- 排版迷之技巧之短线
- "Maximum length exceeded"错误的解决办法
- onConfigurationChanged需要主要的问题
- 工作笔记
- JavaScript事件绑定和普通事件区别
- CSS中的单位px,em,rem
- php跨域
- android 短信拦截并删除
- 正则表达式判断手机号码运营商
- ACM--数学几何--圆环的转动问题--内蒙古14年省赛
- [HDOJ2830]Matrix Swapping II(胡搞)
- cocos2dx刮奖效果实现
- git笔记
- AndroidStudio项目使用SVN需要忽略的文件
- MVC+jQuery.Ajax异步实现增删改查和分页