Simple JAVA Code


  • 1
    public ListNode removeNthFromEnd(ListNode head, int n) {
            int count = 0;
    		ListNode p = head, q = null;
    		while(p != null) {
    			if(++count == n + 1)
    				q = head;
    			else if(count > n + 1)
    				q = q.next;
    			p = p.next;
    		}
    		if(q != null) {
    			q.next = q.next.next;
    			return head;
    		}
    		return head.next;
        }
    

Log in to reply
 

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