My java solution with a hashmap


  • 3
    S
     public void reorderList(ListNode head) {
    	        
    	    HashMap <Integer,ListNode> nodeMap = new HashMap<Integer,ListNode> ();
    	    int len = 0 ;
    	    ListNode p = head ; 
    	    
    	    while (p != null) {
    	    	nodeMap.put(len++, p);
    	    	p = p.next ;
    	    }
    	   
    	    
    	    int i = 0 ; 
    	    int j = len - 1 ;
    	    for (; i < j - 1 ; ++i ,--j) {	    	
    	    	ListNode tmp = nodeMap.get(i).next ;
    	    	nodeMap.get(i).next = nodeMap.get(j);
    	    	nodeMap.get(j).next = tmp ;
    	    	nodeMap.get(j - 1).next = null;
    	    	
    	    }

Log in to reply
 

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