Remove Nth Node From End of List

来源:互联网 发布:图转文软件 编辑:程序博客网 时间:2024/06/08 07:30

/** * 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* out = head;        ListNode* fast = head;        if(head==NULL)            return NULL;        for(int i=0;i<n;i++){            if(fast->next!=NULL)                fast = fast->next;            else                return head->next;                        }                while(fast->next!=NULL){            fast = fast->next;            head = head->next;        }        head->next = head->next->next;        return out;    }};//找到倒数第k+1个数

0 0
原创粉丝点击