码迷,mamicode.com
首页 > 其他好文 > 详细

amazon o2 - reverse second half linked list

时间:2016-01-18 14:57:26      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

public ListNode reverseBetween(ListNode head, int m, int n) {
  if(head==null) return head;
  ListNode slow = head;
  ListNode fast = head;

  //find middle
  while(fast.next!=null) {
    fast = fast.next;
    if(fast.next!=null) {
      fast = fast.next;
    }
    else {
      slow = slow.next;
    }
  }
  ListNode current = slow.next;
  ListNode halfFirst = current;
  if(current == null || current.next==null) {
    return head;
  }
  ListNode next = current.next;

  // reverse
  while(next!=null) {
    ListNode temp = current;
    current = next;
    next = next.next;
    current.next = temp;
  }

  //combine
  slow.next = current;
  halfFirst.next = null;
  return head;
}

amazon o2 - reverse second half linked list

标签:

原文地址:http://www.cnblogs.com/marc-leetcode/p/5139229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!