Simple 6-line Java one-pass solution

  • 8

    public class Solution {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode start=new ListNode(0),slow=start,fast=start;;
        for(int i=0;i<n;i++);
        while(!=null) {;;};


  • 0

    Nice job creating a new list node at the beginning to use as a placeholder.

  • 0

    i don't understand it.why it's i < n, not i < len-n.

  • 0

    The basic idea is the fast pointer is n nodes ahead of slow pointer. So when is null, slow is in the right position that its next node can be removed by statement =;.

Log in to reply

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