Algorithms—92.Reverse Linked List II
2015-09-07 15:26
369 查看
思路:遍历ListNode,存入list中,然后反向
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
List<ListNode> list=f(head, new ArrayList<ListNode>());
list.get(m-1).next=list.get(n-1).next;
for (int i = n-1; i >=m; i--) {
list.get(i).next=list.get(i-1);
}
if (m!=1) {
list.get(m-2).next=list.get(n-1);
return list.get(0);
}else {
return list.get(n-1);
}
}
public List<ListNode> f(ListNode head,List<ListNode> list){
if (head!=null) {
list.add(head);
list=f(head.next,list);
}
return list;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
List<ListNode> list=f(head, new ArrayList<ListNode>());
list.get(m-1).next=list.get(n-1).next;
for (int i = n-1; i >=m; i--) {
list.get(i).next=list.get(i-1);
}
if (m!=1) {
list.get(m-2).next=list.get(n-1);
return list.get(0);
}else {
return list.get(n-1);
}
}
public List<ListNode> f(ListNode head,List<ListNode> list){
if (head!=null) {
list.add(head);
list=f(head.next,list);
}
return list;
}
}
相关文章推荐
- 扩展Django:实现自己的manage命令
- Algorithms—45.Jump Game II
- GO环境设置
- GO备忘录
- 1101 Oh, my God!【错排】
- 【最近公共祖先】[COCI]STOGOVI
- Algorithm --> 爬楼梯求最大分数
- [FFmpeg] 去除 logo
- Algorithms—39.Combination Sum
- google.com 还会换IP
- IE和Google浏览器事件传递参数不同
- Algorithm --> 棋盘中求出A到B的最小步数
- Algorithm --> 求N以内的真分数个数
- django给admin增加功能
- 悟空:用Go语言编写的全文搜索引擎
- 类别 category 以及类扩展
- 认识理解zygote
- 【哥德巴赫猜想】POJ Goldbach's Conjecture 2262
- django中给ajax提交加上csrf
- 【哥德巴赫猜想】LightOJ Goldbach`s Conjecture 1259