Share My solution java using array


  • 0
    S
    public static ListNode reverseKGroup(ListNode head, int k) {
    		ListNode voidHead = new ListNode(0);
    		ListNode tempBef = voidHead;
    		ListNode[] temps = new ListNode[k];
    		voidHead.next = head;enter code here
    		while(head!=null){
    			for(int i = 0;i<k;i++){`enter code here`
    				if(head==null){
    					tempBef.next = temps[0];
    					return voidHead.next;
    				}
    				temps[i] = head;
    				head = head.next;
    			}
    			for(int i = k-1;i>0;i--){
    				temps[i].next = temps[i-1];
    			}
    			tempBef.next = temps[k-1];
    			tempBef = temps[0];
    			
    		}
    		tempBef.next = null;
    		return voidHead.next;
    	}

Log in to reply
 

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