Remove Nth Node From End of List

来源:互联网 发布:时间穿梭机 淘宝 编辑:程序博客网 时间:2024/06/05 04:27
/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* removeNthFromEnd(ListNode* head, int n) {        ListNode *q = head;        int c = n;        while(c-- && q!=NULL){            q = q->next;        }        if(q == NULL) return head->next;        ListNode *p = head;        while(q->next){            p = p->next;            q = q->next;        }        ListNode *temp = p->next;        p->next = temp->next;        delete temp;        return head;    }};
0 0