My code here ,you need to exchange the last one and the frist one,then ,again,so I do this with a function set in LinkedList


  • 0
    D

    My code here ,you need to exchange the last one and the frist one,then ,again,so I do this with a function set in LinkedList

    
    public class reverseLinkedList {
    	 public static void main(String[] args){
    	        LinkedList<Integer> ll=new LinkedList<Integer>();
    	        reverseLinkedList s=new reverseLinkedList();
    	        ll.add(1);
    	        ll.add(2);
    	        ll.add(3);
    	        ll.add(4);
    	        ll.add(5);
    	       LinkedList<Integer> l2= s.reverseBetween(ll,1,5);
    	        for(int i=0;i<l2.size();i++){
    	            System.out.print(l2.get(i)+" ");
    	        }
    	            
    	    }
    	    public LinkedList<Integer> reverseBetween(LinkedList<Integer> l1, int m, int n) {
    	        int a=m-1;
    	        int b=n-1;
    	        while(b-a>0){
    	            int temp=l1.get(a);
    	            l1.set(a, l1.get(b));
    	            l1.set(b, temp);
    	            a++;
    	            b--;
    	        }
    			return l1;
    	        
    	    }
    	   
    }
    

Log in to reply
 

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