Simple Java solution


  • 0
    S
    public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode pre = head,curr = head,end = head, ret = head;
            for(int i = 0;i<n;i++) {
                if(end == null) return null;
                end = end.next;
            }
            if(end == null) {
                return head.next;
            }
            while(end!=null) {
                pre = curr;
                curr = curr.next;
                end = end.next;
            }
            pre.next = curr.next;
            return ret;
        }
    
    

Log in to reply
 

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