This is my JAVA code,please give me some suggestion to improve it,thx


  • 0
    K

    public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {

        ListNode ln = head;
    	ListNode start = head;
    	int list_length = 0;
    	
    	while(ln!=null){
    		list_length++;
    		ln = ln.next;
    	}
    	if(m<1||m>n||n>list_length){
    	    return head;
    	}
    	
    	int[] array = new int[list_length];
    	int i = 0;
    	ln = head;
    	
    	while(ln != null){
    		array[i] = ln.val;
    		ln = ln.next;
    		i++;
    	}
    	for(int j = 0; j < m-1; j++){
    		start = start.next;
    	}
    	while(n >= m){
    		start.val = array[n-1];
    		start = start.next;
    		n--;
    	}
    	return head;
    }
    

    }


  • 0
    D

    Use Array to solve this problem is easy. I used Stack and it also worked.


Log in to reply
 

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