JAVA------------Easy Version To Understand!!!!!!!!!!!!!!!!!


  • 0
    H
    	public ListNode removeNthFromEnd(ListNode head, int n) {
    	if (head == null)
    		return null;
    	ListNode headCount = new ListNode(0);
    	headCount.next = head;
    	head = headCount;
    	ListNode tmp = head, slow = head.next, fast = head.next;
    	int count = 1;
    	while (count < n) {
    		count++;
    		fast = fast.next;
    	}
    	while (fast.next != null) {
    		fast = fast.next;
    		slow = slow.next;
    		tmp = tmp.next;
    	}
    	tmp.next = slow.next;
    
    	return head.next;
    }

  • 0
    J

    Great! But I don't think you need to use temp.


Log in to reply
 

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