struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { structListNode* dummy =malloc(sizeof(struct ListNode)); dummy->val = 0, dummy->next = head; structStack* stk =NULL; structListNode* cur = dummy; while (cur) { structStack* tmp =malloc(sizeof(struct Stack)); tmp->val = cur, tmp->next = stk; stk = tmp; cur = cur->next; } for (int i = 0; i < n; ++i) { structStack* tmp = stk->next; free(stk); stk = tmp; } structListNode* prev = stk->val; prev->next = prev->next->next; structListNode* ans = dummy->next; free(dummy); return ans; }
复杂度分析
时间复杂度:O(L),其中 L 是链表的长度。
空间复杂度:O(L),其中 L 是链表的长度。主要为栈的开销。
方法三:双指针
思路与算法
我们也可以在不预处理出链表的长度,以及使用常数空间的前提下解决本题。
由于我们需要找到倒数第 n 个节点,因此我们可以使用两个指针 first 和 second 同时对链表进行遍历,并且 first 比 second 超前 n 个节点。当 first 遍历到链表的末尾时,second 就恰好处于倒数第 n 个节点。
具体地,初始时 first 和 second 均指向头节点。我们首先使用 first 对链表进行遍历,遍历的次数为 n。此时,first 和 second 之间间隔了 n-1 个节点,即 first 比 second 超前了 n 个节点。
在这之后,我们同时使用 first 和 second 对链表进行遍历。当 first 遍历到链表的末尾(即 first 为空指针)时,second 恰好指向倒数第 n 个节点。
根据方法一和方法二,如果我们能够得到的是倒数第 n 个节点的前驱节点而不是倒数第 n 个节点的话,删除操作会更加方便。因此我们可以考虑在初始时将 second 指向哑节点,其余的操作步骤不变。这样一来,当 first 遍历到链表的末尾时,second 的下一个节点就是我们需要删除的节点。
代码
[sol3-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: ListNode* removeNthFromEnd(ListNode* head, int n){ ListNode* dummy = newListNode(0, head); ListNode* first = head; ListNode* second = dummy; for (int i = 0; i < n; ++i) { first = first->next; } while (first) { first = first->next; second = second->next; } second->next = second->next->next; ListNode* ans = dummy->next; delete dummy; return ans; } };
[sol3-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNodedummy=newListNode(0, head); ListNodefirst= head; ListNodesecond= dummy; for (inti=0; i < n; ++i) { first = first.next; } while (first != null) { first = first.next; second = second.next; } second.next = second.next.next; ListNodeans= dummy.next; return ans; } }
[sol3-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defremoveNthFromEnd(self, head: ListNode, n: int) -> ListNode: dummy = ListNode(0, head) first = head second = dummy for i inrange(n): first = first.next
while first: first = first.next second = second.next second.next = second.next.next return dummy.next
[sol3-Golang]
1 2 3 4 5 6 7 8 9 10 11 12
funcremoveNthFromEnd(head *ListNode, n int) *ListNode { dummy := &ListNode{0, head} first, second := head, dummy for i := 0; i < n; i++ { first = first.Next } for ; first != nil; first = first.Next { second = second.Next } second.Next = second.Next.Next return dummy.Next }
[sol3-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { structListNode* dummy =malloc(sizeof(struct ListNode)); dummy->val = 0, dummy->next = head; structListNode* first = head; structListNode* second = dummy; for (int i = 0; i < n; ++i) { first = first->next; } while (first) { first = first->next; second = second->next; } second->next = second->next->next; structListNode* ans = dummy->next; free(dummy); return ans; }