GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — 删除链表的倒数第N个节点-19 · Issue #46 · sl1673495/leetcode-javascript Open Labels Description给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 示例: 说明: 给定的 n 保证是有效的。 进阶: 你能尝试使用一趟扫描实现吗? 来源:力扣(LeetCode) 思路数组法由于不知道链表有几个节点,所以先建立一个数组,用来记录节点对应的下标顺序。 求 /** * @param {ListNode} head * @param {number} n * @return {ListNode} */ let removeNthFromEnd = function (head, n) { let node = head let nodes = [] do { nodes.push(node) node = node.next } while (node) const l = nodes.length const index = l - n const targetNode = nodes[index] const prevNode = nodes[index - 1] if (prevNode) { prevNode.next = targetNode.next } const tempNext = targetNode.next targetNode.next = null if (targetNode === head) { return tempNext } return head } ActivityMetadataMetadataAssigneesLabelsProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |