找到待删除节点的前一个节点,让它指向删除节点的后一个节点

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
        ListNode pre = head;
        int del = length(head) - n;
        if(del == 0){
            head = head.next;
            return head;
        }
        for(int i=0;i<del-1;i++){
            pre = pre.next;
        }
        ListNode m = pre.next.next;
        pre.next = m;
        return head;
    }
    public int length(ListNode head){
        int flag = 0;
        while(head != null){
            flag++;
            head = head.next;
        }
        return flag;
    }
}