首页 > 其他 > 详细

amazon o2 - reverse second half linked list

时间:2016-01-18 14:57:17      阅读:241      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!