class Solution {
public:
int getLength(ListNode* head) {
int length = 0;
while (head) {
++length;
head = head->next;
}
return length;
}
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0, head);
int length = getLength(head);
ListNode* cur = dummy;
for (int i = 1; i < length - n + 1; ++i) {
cur = cur->next;
}
cur->next = cur->next->next;
ListNode* ans = dummy->next;
delete dummy;
return ans;
}
};
问:ListNode* dummy = new ListNode(0, head);这里的head既然指的是next,那为什么cur->next不是cur—>head。
问:ListNode* ans = dummy->next;为什么要单独设立一个ans指针,此时cur为什么不用呢,我觉得ans多余
问:dummy会随着cur = cur->next;的移动而移动吗,为什么
问:ListNode* ans = dummy->next;这一段有什么意义,他只是单纯指向链表中的一个点啊,他能代指能个修改后的链表吗,为什么
来个负责任的大佬