Java code without have to find the length when k is less than length


  • 0
    Y

    '''
    if (head == null || k == 0) {
    return head;
    }
    ListNode new_head = null, fast = head, next = null, slow = head;
    int i;
    for (i = 0; i < k; i++) {
    if (fast.next == null) {
    fast = head;
    k = k % (i + 1);
    if (k == 0) {
    return head;
    }
    i = 0;
    }
    fast = fast.next;
    }
    while (fast.next != null) {
    fast = fast.next;
    slow = slow.next;
    }

    	new_head = slow.next;
    	next = slow.next;
    	slow.next = null;
    	slow = next;
    	while (slow.next != null) {
    		slow = slow.next;
    	}
    	slow.next = head;
    
    	return new_head;
    

    '''


Log in to reply
 

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