Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255374
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-09-21 16:51:40

//Reverse Linked List II My Submissions Question Solution 
//Total Accepted: 50939 Total Submissions: 195918 Difficulty: Medium
//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->NULL, m = 2 and n = 4,
//
//return 1->4->3->2->5->NULL.
//
//Note:
//Given m, n satisfy the following condition:
//1 ≤ m ≤ n ≤ length of list.
public class ReverseLinkedListII {


public static void main(String[] args) {
// TODO Auto-generated method stub


}
public ListNode reverseBetween(ListNode head, int m, int n) {
       if(m>=n)
      return head;
       ListNode prehead=new ListNode(0);
       prehead.next=head;
       //p对应i 得到前一个
       ListNode p=prehead;
       int i;
       for(i=0;i<m-1&&p!=null;i++){
      p=p.next;
       }
       if(p==null){
      return head;
       }
       //p表示前一个
       ListNode first=p.next;
       ListNode s=p.next;
        //保存当前要插入的值
       ListNode pres;
       for(;i<n&&s!=null;i++){
      pres=s;
      s=s.next;
      pres.next=p.next;
      p.next=pres;
   
       }
       first.next=s;
       return prehead.next;
    }
}

阅读(369) | 评论(0) | 转发(0) |
0

上一篇:SubSetTwo

下一篇:Restore IP Addresses

给主人留下些什么吧!~~