首页 > 代码库 > LeetCode Remove Nth Node From End of List

LeetCode Remove Nth Node From End of List

class Solution {public:    ListNode *removeNthFromEnd(ListNode *head, int n) {        if (head == NULL) return NULL;        ListNode* pre = NULL;        ListNode* cur = head;        ListNode* fast= cur;                for (int i=1; i<n; i++) {            fast = fast->next;        }                while (fast->next != NULL) {            pre = cur;            cur = cur->next;            fast= fast->next;        }                if (pre == NULL) {            return head->next;        } else {            pre->next = cur->next;            return head;        }    }};

一次过呗