[No Extra Pointers or Loop Needed] My Java Recursive Solution with O(n).

  • 2
    public class Solution {
        public int remove(ListNode node, int n){
            if(node.next == null) return n-1;
            int rest = remove(node.next, n);
            if(rest == 0){
                node.next = (node.next).next;
                return -1;
                return rest-1;
        public ListNode removeNthFromEnd(ListNode head, int n) {
            return (remove(head, n) == 0)? head.next : head;

Log in to reply

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