So wierd!!!!my Java code!the code will be right online,but will be wrong in my machine


  • 0
    1
    public static ListNode reverseBetween(ListNode head, int m, int n) {
    	if(head==null||m>n) return null;
    	if(m==n) return head;
    	ListNode fakeHead=new ListNode(0);
    	fakeHead.next=head;
    	ListNode temp=fakeHead;
    	ListNode pre=null;
    	int s=0;
    	int e=0;
    	boolean flag=false;
    	ListNode last=head;
    	while(temp!=null&&e<n&&last!=null){
    		if(flag){
    			ListNode preNext=pre.next;
    			ListNode tempNext=temp.next;
    			last=tempNext.next;
    			pre.next=tempNext;
    			tempNext.next=preNext;
    			temp.next=last;
    			e++;
    			continue;
    		}
    		if(s==m-1){
    			pre=temp;
    			flag=true;
    		}
    		temp=temp.next;
    		s++;
    		e++;
    	}
        return fakeHead.next;
    }
    

    // this is very bizarre,if e<n the code will be right online,but be wrong in my machine
    // if e<=n,the code will be right in my machine,but be wrong online
    // while(temp!=null&&e<=n&&last!=null){


Log in to reply
 

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