19. 删除链表的倒数第 N 个结点
给你一个链表,删除链表的倒数第 n
个结点,并且返回链表的头结点。
示例 1:
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5]
示例 2:
输入:head = [1], n = 1 输出:[]
示例 3:
输入:head = [1,2], n = 1 输出:[1]
提示:
- 链表中结点的数目为
sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
解法一:先算长度,再用长度减去n即为要删除的节点
/** * 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 head, int n) { int length = 0; ListNode dummy = new ListNode(-1, head); ListNode pre = dummy; while (head != null) { length++; head = head.next; } for (int i = 0; i < length - n;i++) { pre = pre.next; } pre.next = pre.next.next; return dummy.next; } }
解法二:双指针,先让快指针比慢指针快n步,然后共同以相同速度移动,当快指针移动至最后一个节点时,慢指针所指向的即为倒数第n个节点
public ListNode removeNthFromEnd(ListNode head, int n){ ListNode dummyNode = new ListNode(0); dummyNode.next = head; ListNode fastIndex = dummyNode; ListNode slowIndex = dummyNode; //只要快慢指针相差 n 个结点即可 for (int i = 0; i < n ; i++){ fastIndex = fastIndex.next; } while (fastIndex.next != null){ fastIndex = fastIndex.next; slowIndex = slowIndex.next; } //此时 slowIndex 的位置就是待删除元素的前一个位置。 //具体情况可自己画一个链表长度为 3 的图来模拟代码来理解 slowIndex.next = slowIndex.next.next; return dummyNode.next; }
标签:head,ListNode,val,19,next,链表,int,倒数第 From: https://www.cnblogs.com/fulaien/p/16871632.html