leetcode --19. Remove Nth Node From End of List

来源:互联网 发布:淘宝游戏光盘 编辑:程序博客网 时间:2024/06/10 07:33

题目:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

代码:

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

原创粉丝点击