My simple java solution


  • 8
    V
    public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast = head;
        ListNode slow = head;
    	while(fast != null) {
    		fast = fast.next;
        	if(n-- < 0) {
        		slow = slow.next;
        	}
        }
    	
    	if(n == 0) {
    		head = head.next;
    	} else if(n < 0) {
    		slow.next = slow.next.next;
    	} else {
    		return null;
    	}
    	
    	return head;
    }
    

    }


Log in to reply
 

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