solution.json 2.5 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "a4a05fa8ae2043028026f1aeb8d31b26",
  "keywords": "链表,双指针",
  "title": "删除链表的倒数第 N 个结点",
  "desc": [
    {
      "content": "\n<p>给你一个链表,删除链表的倒数第 <code>n</code><em> </em>个结点,并且返回链表的头结点。</p><p><strong>进阶:</strong>你能尝试使用一趟扫描实现吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0019.Remove%20Nth%20Node%20From%20End%20of%20List/images/remove_ex1.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,2,3,4,5], n = 2<strong><br />输出:</strong>[1,2,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [1], n = 1<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2], n = 1<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中结点的数目为 <code>sz</code></li>\t<li><code>1 <= sz <= 30</code></li>\t<li><code>0 <= Node.val <= 100</code></li>\t<li><code>1 <= n <= sz</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode() {\n\t}\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\nclass Solution {\n\tpublic ListNode removeNthFromEnd(ListNode head, int n) {\n\t\tListNode v = new ListNode(0, head);\n\t\tListNode handle = v;\n\t\tList<ListNode> index = new ArrayList<>();\n\t\twhile (v != null) {\n\t\t\tindex.add(v);\n\t\t\tv = v.next;\n\t\t}\n\t\tint pre = index.size() - n - 1;\n\t\tint next = index.size() - n + 1;\n\t\tindex.get(pre).next = next >= 0 && next < index.size() ? index.get(next) : null;\n\t\treturn handle.next;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-ccc2cd1afb9b4c05abec6e80d581bfd2",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600469818"
}