Time limit exceed


  • 0

    Can anyone help me to look at my code?
    public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
    if(head == null || head.next == null) return head;
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode cur = dummy, pre = null;
    for(int i = 0; i< m; i++){
    pre = cur;
    cur = cur.next;
    }
    //left_end points to the head of reversed list
    ListNode left_end = pre;
    ListNode end = cur;
    //reverse
    for(int i = n; i < m; i++){
    pre = cur;
    cur = cur.next;
    cur.next = pre;
    }
    //connect
    left_end.next = pre;
    end.next = cur;
    return dummy.next;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.