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

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

时间:2016-01-24 15:34:19      阅读:122      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!