作者: 
buyfood (buyfood)   
2024-02-23 17:25:05各位高手大家好,今天練習這題時有些疑惑想請教
題目:https://leetcode.com/problems/remove-nth-node-from-end-of-list/descripti
on/
一開始寫出的做法如下:
```
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        static int i = 0;
        if (head->next != NULL)
        {
            head->next = removeNthFromEnd(head->next, n);
        }
        i++;
        if (n == i)
        {
            return head->next; // skip yourself
        }
        else
        {
            return head;
        }
    }
};
```
在Leetcode上無法通過([1], 1)與([1,2], 1)這兩個測試
回傳結果是[1]與[1,2],也就是並沒有成功移除node
我知道這段code並沒有實際刪除node,只是忽略掉
但走訪起來應該跟正確答案相符,且在vs上測試可以得到正確答案
目前已知:
1. 不建議用static變數
2. 會有mem leak的問題
但神奇的事情發生了,我把static換成call by reference的做法,就通過leetcode的測
試了?!?
```
class Solution {
public:
  ListNode* removeNthFromEnd2(ListNode* head, int& n) {
    if (head->next != NULL)
    {
      head->next = removeNthFromEnd2(head->next, n);
    }
    n