Leetcode19

题目:

给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。

示例:

给定一个链表: 1->2->3->4->5, 和 n = 2.

当删除了倒数第二个节点后,链表变为 1->2->3->5.
说明:

给定的 n 保证是有效的。

进阶:

你能尝试使用一趟扫描实现吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一: 前后指针(快慢指针)

参考链接:
https://www.cnblogs.com/tianrunzhi/p/10416629.html
前指针先走n步,然后前、后指针同时走,当前指针走到节点尾时,后指针刚好走到要删除的节点。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x

你可能感兴趣的:(Leetcode19)