19. Remove Nth Node From End of List

来源:互联网 发布:每日一文 知乎 编辑:程序博客网 时间:2024/04/29 13:21
ListNode* removeNthFromEnd(ListNode* head, int n) {    ListNode* first = head;    ListNode* second = head;    int i = 0;    while (second) {        i++;        second = second->next;    }    int j= 0;    if(i==n) return head->next;    while (j < i-n -1) {        first = first->next;        j++;    }    first->next = first->next->next;    return head;}

0 0
原创粉丝点击