首页 > 其他 > 详细

Jan 23 - Reverse Linked List; Linked List; Pointers;

时间:2016-01-24 15:32:13      阅读:152      评论:0      收藏:0      [点我收藏+]
/**
 * 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) {
        ListNode cur = head;
        ListNode prev = null;
        int i = 1;
        while(i < m){
            prev = cur;
            cur = cur.next;
            i++;
        }
        ListNode start = cur, end = cur;
        while(i < n){
            ListNode nex = end.next.next;
            end.next.next = start;
            start = end.next;
            end.next = nex;
            cur = nex;
            i++;
        }
        if(prev != null){
            prev.next = start;
            return head;
        }
        return start;
    }
}

Too late night now, no energy to say anything...Just pointer operation.

Jan 23 - Reverse Linked List; Linked List; Pointers;

原文:http://www.cnblogs.com/5683yue/p/5155142.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!