给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode before = head; ListNode after = head; if(head==null){ return null; } while(n!=0){ before = before.next; n--; } if(before==null){//删除头结点 return head.next; } while(before.next!=null){ before = before.next; after = after.next; } after.next = after.next.next;//删除中间结点 return head; }