/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode p, int n) {
ListNode s = new ListNode(-1, p);
pro(s, n);
return s.next;
}
public int pro(ListNode p, int val) {
if (p == null) {
return 0;
}
int nth = pro(p.next, val);
if (nth == val) {
p.next = p.next.next;
}
return nth + 1;
}
}
LeetCode-19:删除链表的倒数第N个结点(Java语言实现)
最新推荐文章于 2024-08-10 22:52:22 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)