您的位置:首页 > Web前端 > Node.js

LeetCode: Remove Nth Node From End of List

2013-09-29 11:06 447 查看
不考虑节点不存在的情况

class Solution {

public:

    ListNode *removeNthFromEnd(ListNode *head, int n) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        ListNode *cur = head,*pri=head;

        while(n--){

            cur = cur->next;

        }

        if(cur==NULL)

            return head->next;

        while(cur->next!=NULL){

            cur = cur->next;

            pri = pri->next;

        }

        pri->next = pri->next->next;

        return head;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: