image-20220320215322894.png

    双指针

    1. class Solution {
    2. public ListNode removeNthFromEnd(ListNode head, int n) {
    3. ListNode dummy = new ListNode(0, head);
    4. ListNode first = head;
    5. ListNode second = dummy;
    6. for (int i = 0; i < n; ++i) {
    7. first = first.next;
    8. }
    9. while (first != null) {
    10. first = first.next;
    11. second = second.next;
    12. }
    13. second.next = second.next.next;
    14. ListNode ans = dummy.next;
    15. return ans;
    16. }
    17. }

    下周好好学习!!