给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
![](https://img-blog.csdnimg.cn/img_convert/2ba5d38d79effbe6b897737bc5400fb8.png#align=left&display=inline&height=222&margin=[object Object]&originHeight=222&originWidth=542&size=0&status=done&style=none&width=542)
示例1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例2:
输入:head = [1], n = 1
输出:[]
示例3:
输入:head = [1,2], n = 1
输出:[1]
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
暴力解法
![image.png](https://img-blog.csdnimg.cn/img_convert/7a831f7ec501374a5c28e92d8891ca6c.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=15921&status=done&style=none&width=640)
![image.png](https://img-blog.csdnimg.cn/img_convert/bd09b85575a402ecd262d0a26b006219.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=15750&status=done&style=none&width=640)
![image.png](https://img-blog.csdnimg.cn/img_convert/3a1fafc4fea9cc20ff9da01ca6d2b9d3.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=15856&status=done&style=none&width=640)‘![image.png](https://img-blog.csdnimg.cn/img_convert/6d73e1903477db89a73cd2856e0efa09.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=15916&status=done&style=none&width=640)
![image.png](https://img-blog.csdnimg.cn/img_convert/418027d0411d478289ee660a1729f945.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=15903&status=done&style=none&width=640)
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
int length = getLength(head);
ListNode cur = dummy;
for (int i = 1; i < length - n + 1; ++i) {
cur = cur.next;
}
cur.next = cur.next.next;
ListNode ans = dummy.next;
return ans;
}
public int getLength(ListNode head) {
int length = 0;
while (head != null) {
++length;
head = head.next;
}
return length;
}
双指针
![image.png](https://img-blog.csdnimg.cn/img_convert/331b4f092957ba700f8a1b895a127e38.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=17012&status=done&style=none&width=640)
![image.png](https://img-blog.csdnimg.cn/img_convert/4d3bcb24b739515fc734dd6b3f2c0c41.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=15656&status=done&style=none&width=640)
![image.png](https://img-blog.csdnimg.cn/img_convert/7c779f5af7a66b37d7dcb3f62c1cc2d6.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=15977&status=done&style=none&width=640)
![image.png](https://img-blog.csdnimg.cn/img_convert/203d8c97bd497d1af95c414fd0b50f97.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=16031&status=done&style=none&width=640)
![image.png](https://img-blog.csdnimg.cn/img_convert/d986ed03eda81a129e39efc33a38e68d.png#align=left&display=inline&height=360&margin=[object Object]&name=image.png&originHeight=360&originWidth=640&size=16163&status=done&style=none&width=640)
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = head;
ListNode slow = dummy;
for (int i = 0; i < n; i++) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}