算法-链表-双指针-删除倒数第n个
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
给一个链表,删除倒数第n个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode q = dummy, p = dummy;
while(n-->-1){
p = p.next;
}
while(p!=null){
q = q.next;
p = p.next;
}
q.next = q.next.next; // 优化:释放被删除节点
return dummy.next;
}
}
评论:
技术文章推送
手机、电脑实用软件分享
微信公众号:AndrewYG的算法世界