My java intuitive solution


  • 0
    A
        public ListNode removeNthFromEnd(ListNode head, int n) {
            if(head == null || head.next == null){
                return null;
            }
            ListNode precursor = head;
            ListNode leader = head.next;
            int i = 1;
            while(i<n && leader.next!= null){
                leader = leader.next;
                ++i;
            }
            while(leader.next!=null){
                ++i;
                leader = leader.next;
                precursor = precursor.next;
            }
            if(i == --n){
                return head.next;
            }
            precursor.next = precursor.next.next;
        return head;
        }
    }

Log in to reply
 

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