题目描述

原题链接

示例 1:
image.png
输入: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

个人解法

JavaScript

解法1

两次扫描

  1. /*
  2. * @lc app=leetcode.cn id=19 lang=javascript
  3. *
  4. * [19] 删除链表的倒数第 N 个结点
  5. */
  6. // @lc code=start
  7. /**
  8. * Definition for singly-linked list.
  9. * function ListNode(val, next) {
  10. * this.val = (val===undefined ? 0 : val)
  11. * this.next = (next===undefined ? null : next)
  12. * }
  13. */
  14. /**
  15. * @param {ListNode} head
  16. * @param {number} n
  17. * @return {ListNode}
  18. */
  19. var removeNthFromEnd = function (head, n) {
  20. let len = 0;
  21. let temp = head;
  22. while (temp) {
  23. temp = temp.next;
  24. len++;
  25. }
  26. const target = len - n;
  27. if (target === 0) {
  28. return head.next;
  29. }
  30. temp = head;
  31. for (let i = 0; i < target - 1; i++) {
  32. temp = temp.next;
  33. }
  34. temp.next = temp.next.next;
  35. return head;
  36. };
  37. // @lc code=end

解法2

1次扫描

  1. /*
  2. * @lc app=leetcode.cn id=19 lang=javascript
  3. *
  4. * [19] 删除链表的倒数第 N 个结点
  5. */
  6. // @lc code=start
  7. /**
  8. * Definition for singly-linked list.
  9. * function ListNode(val, next) {
  10. * this.val = (val===undefined ? 0 : val)
  11. * this.next = (next===undefined ? null : next)
  12. * }
  13. */
  14. /**
  15. * @param {ListNode} head
  16. * @param {number} n
  17. * @return {ListNode}
  18. */
  19. var removeNthFromEnd = function (head, n) {
  20. let len = 0;
  21. let temp = head;
  22. const arr = [];
  23. while (temp) {
  24. arr.push(temp);
  25. temp = temp.next;
  26. len++;
  27. }
  28. const target = len - n;
  29. if (target === 0) {
  30. return head.next;
  31. }
  32. temp = arr[target - 1];
  33. temp.next = temp.next.next;
  34. return head;
  35. };
  36. // @lc code=end

Java

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode removeNthFromEnd(ListNode head, int n) {
  13. ListNode p = head;
  14. int count = 1;
  15. while (p.next != null) {
  16. p = p.next;
  17. count++;
  18. }
  19. p = head;
  20. if (count == n) {
  21. head = p.next;
  22. } else {
  23. for (int i = 0; i < count - n-1; i++) {
  24. p = p.next;
  25. }
  26. p.next = p.next.next;
  27. }
  28. return head;
  29. }
  30. }

其他解法

Java

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode removeNthFromEnd(ListNode head, int n) {
  13. ListNode ln1 = new ListNode(-1, head);
  14. ListNode p = ln1,q=ln1;
  15. for (int i=0;i<n;i++){
  16. q = q.next;
  17. }
  18. while (q.next != null) {
  19. p=p.next;
  20. q=q.next;
  21. }
  22. if (p.next==head) {
  23. head=head.next;
  24. } else {
  25. p.next = p.next.next;
  26. }
  27. return head;
  28. }
  29. }

JavaScript

  1. /*
  2. * @lc app=leetcode.cn id=19 lang=javascript
  3. *
  4. * [19] 删除链表的倒数第 N 个结点
  5. */
  6. // @lc code=start
  7. /**
  8. * Definition for singly-linked list.
  9. * function ListNode(val, next) {
  10. * this.val = (val===undefined ? 0 : val)
  11. * this.next = (next===undefined ? null : next)
  12. * }
  13. */
  14. /**
  15. * @param {ListNode} head
  16. * @param {number} n
  17. * @return {ListNode}
  18. */
  19. var removeNthFromEnd = function(head, n) {
  20. let ret = new ListNode(0, head),
  21. slow = fast = ret;
  22. while(n--) fast = fast.next;
  23. if(!fast) return ret.next;
  24. while (fast.next) {
  25. fast = fast.next;
  26. slow = slow.next
  27. };
  28. slow.next = slow.next.next;
  29. return ret.next;
  30. };
  31. // @lc code=end