LeetCode2.2.2(Reverse Linked List II)
2015-08-29 09:34
316 查看
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example: Given 1->2->3->4->5->nullptr, m = 2 and n = 4,
return 1->4->3->2->5->nullptr.
Note: Given m, n satisfy the following condition: 1 m n length of list.
这道题思路不难,关键是要找准边界条件
For example: Given 1->2->3->4->5->nullptr, m = 2 and n = 4,
return 1->4->3->2->5->nullptr.
Note: Given m, n satisfy the following condition: 1 m n length of list.
这道题思路不难,关键是要找准边界条件
public static void solution_2_2_2(Node head,int m,int k){ Node temp = head, mNode = null, kNode = null, mPre = null, h = null; for (int i = 1; i <= k; i++) { if (i == m - 1) mPre = temp; if (i == m) mNode = temp; if (i == k) kNode = temp; temp = temp.next; } // if(mPre!=null){ temp = mNode; Node t = null, p = null; for (int i = m; i <= k; i++) { p = temp.next; if (t == null) { t = temp; t.next = kNode.next; } else { temp.next = t; t = temp; } if (i == k && mPre != null) { mPre.next = t; h = head; } if (i == k && mPre == null) { h = t; } temp = p; } for (Node r = h; r != null; r = r.next) { System.out.print(r.data + " "); } }
相关文章推荐
- jsp过滤器
- Struts(3)的工作机制及分析
- Struts(2)环境搭建
- 线程的join方法
- 队列小述
- SpringMVC详解(一)简介
- Spring MVC详解(二)Spring Web MVC是什么
- Spring MVC详解(三)DispatcherServlet作用
- 操作注册表
- java 死锁产生原因及解锁
- iOS 开发常见的错误提示信息
- Spring MVC详解(四) Controller接口控制器详解(1)
- Spring MVC详解(四)接口控制器详解(2)
- Spring MVC详解(四)Controller接口控制器详解(3)
- Spring MVC详解(四)Controller接口控制器详解 (4)
- Vmware-桥接模式连接网络
- MySQL索引背后的数据结构及算法原理
- 动态规划之背包问题(一)
- Spring MVC详解(四)Controller接口控制器详解(5)
- Spring MVC详解(四)Controller接口控制器详解(6)