{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "7f3fdaec1a3d4246b86533764656de26",
  "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": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class ListNode:\n\tdef __init__(self, val=0, next=None):\n\t\tself.val = val\n\t\tself.next = next\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution:\n\tdef removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:\n\t\tv = ListNode(0, head)\n\t\thandle = v\n\t\tindex = []\n\t\twhile v is not None:\n\t\t\tindex.append(v)\n\t\t\tv = v.next\n\t\tpre = len(index)-n-1\n\t\tnext = len(index)-n+1\n\t\tindex[pre].next = index[next] if next >= 0 and next < len(\n\t\t\tindex) else None\n\t\treturn handle.next\n# %%\nl = LinkList()\nlist1 = [1,2,3,4,5]\nhead = l.initList(list1)\nn = 2\ns = Solution()\nprint(l.convert_list(s.removeNthFromEnd(head, n)))",
    "language": "python"
  },
  "node_id": "dailycode-15dfcfc57f144a59b0745657fd22e7de",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600469818"
}