{"id":2800,"date":"2020-01-21T12:38:34","date_gmt":"2020-01-21T04:38:34","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2800"},"modified":"2020-01-21T12:38:34","modified_gmt":"2020-01-21T04:38:34","slug":"%e3%80%90leetcode%e3%80%910019-%e5%88%a0%e9%99%a4%e9%93%be%e8%a1%a8%e7%9a%84%e5%80%92%e6%95%b0%e7%ac%acn%e4%b8%aa%e7%bb%93%e7%82%b9","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2800\/","title":{"rendered":"\u3010LeetCode\u30110019 \u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2cN\u4e2a\u7ed3\u70b9"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/remove-nth-node-from-end-of-list\/\">https:\/\/leetcode-cn.com\/problems\/remove-nth-node-from-end-of-list\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-<em>\n<\/em># Definition for singly-linked list.\nclass ListNode(object):\n    def __init__(self, x):\n        self.val = x\n        self.next = None\nclass Solution(object):\n    def removeNthFromEnd(self, head, n):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> head: ListNode\n        <\/em><strong><em>:type<\/em><\/strong><em> n: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: ListNode\n        \"\"\"\n        <\/em>if head is None:\n            return\n        node = head\n        link_list_len = 0\n        while node is not None:\n            link_list_len += 1\n            node = node.next\n        if link_list_len &lt; n:\n            return head\n        if link_list_len == n:\n            return head.next\n        node = head\n        pre = node\n        for i in range(link_list_len - n):\n            pre = node\n            node = node.next\n        pre.next = node.next\n        return head\nif __name__ == '__main__':\n    n5 = ListNode(5)\n    n4 = ListNode(4)\n    n3 = ListNode(3)\n    n2 = ListNode(2)\n    head = ListNode(1)\n    head.next = n2\n    n2.next = n3\n    n3.next = n4\n    n4.next = n5\n    print(Solution().removeNthFromEnd(head, 1))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u9996\u5148\u904d\u5386\u5168\u90e8\u957f\u5ea6\u4ee5\u83b7\u53d6\u94fe\u8868\u957f\u5ea6\uff0c\u7136\u540e\u518d\u904d\u5386\u5230\u5012\u6570\u4f4en\u4e2a\u7ed3\u70b9\u5220\u9664\u5373\u53ef\u3002\u9700\u8981\u6ce8\u610f\u7684\u5c31\u662f\u5bf9\u4e8e\u957f\u5ea6\u4e3an\u7ed3\u70b9\u7684\u94fe\u8868\u5220\u9664\u5012\u6570\u7b2cn\u4e2a\u2014\u2014\u4e5f\u5c31\u662f\u5220\u9664\u7b2c\u4e00\u4e2a\u7684\u60c5\u51b5\uff0c\u76f4\u63a5\u8fd4\u56dehead\u7684next\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/remove-nth-node-f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1272,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2800"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2800"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2800\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2800"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}