{ "question_id": 19, "question_title": "删除链表的倒数第 N 个结点", "difficulty": "中等", "question_content": "<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>", "topic_link": "https://bbs.csdn.net/topics/600469818", "cpp": "struct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\n#include <vector>\nclass Solution\n{\npublic:\n\tListNode *removeNthFromEnd(ListNode *head, int n)\n\t{\n\t\tListNode empty_node(0, head);\n\t\tListNode *p = &empty_node;\n\t\tstd::vector<ListNode *> pv;\n\t\twhile (p != nullptr)\n\t\t{\n\t\t\tpv.push_back(p);\n\t\t\tp = p->next;\n\t\t}\n\t\tp = pv[pv.size() - 1 - n];\n\t\tp->next = p->next->next;\n\t\treturn empty_node.next;\n\t}\n};", "java": "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}", "js": "/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @param {number} n\n * @return {ListNode}\n */\n var removeNthFromEnd = function(head, n) {\n let v = {val:0, next: head};\n\tlet handle = v;\n\tlet index = [];\n\twhile(v!=null){\n\t\tindex.push(v);\n\t\tv = v.next;\n\t}\n\n\tconst pre = index.length-n-1;\n\tconst next = index.length-n+1;\n\tindex[pre].next = index[next]===undefined?null:index[next];\n\t\n return handle.next;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[\n\t\t\t{val:1},\n\t\t\t1,\n\t\t\tundefined\n\t\t],\n\n\t\t[\n\t\t\t{val:1,next:{val:1}},\n\t\t\t2,\n\t\t\t{val:1}\n\t\t],\n\n\t\t[\n\t\t\t{val:1,next:{val:2,next:{val:3,next:{val:4,next:{val:5}}}}},\n\t\t\t2,\n\t\t\t{val:1,next:{val:2,next:{val:3,next:{val:5}}}},\n\t\t]\n\t];\t\n\tfor(const testCase of testCases){\n\t\tconst output = removeNthFromEnd(testCase[0], testCase[1]);\n\t\tif(JSON.stringify(output)===JSON.stringify(testCase[2])){\n\t\t\tconsole.log(`[OK]`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]`);\n\t\t\tprocess.exit(1);\n\t\t}\n\t}\n}\n\nmain();\n", "python": "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)))", "status": 1, "keywords": "链表,双指针", "license": { "cpp": "csdn.net", "python": "csdn.net", "java": "csdn.net" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/18/18_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/18/18_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/18/18_java.ipynb?type=file" }, "notebook_enable": 1 }