My java solution with 1ms


  • 1
    W
    public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode l1 = head;  
        ListNode l2 = l1;     
        int i = 0;
        while (i < n) {
            l1 = l1.next;
            i++;
        }
        if (l1 == null) {
            return head.next;
        }
        while (l1.next != null) {
            l1 = l1.next;
            l2 = l2.next;
        }
        l2.next = l2.next.next;
        return head;
    }
    

    }


Log in to reply
 

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