首页 > 其他 > 详细

LeetCode "Remove Nth Node From End of List"

时间:2014-07-23 14:52:16      阅读:309      评论:0      收藏:0      [点我收藏+]

Another Double pointer solution. 1A!

class Solution {
public:
    ListNode *removeNthFromEnd(ListNode *head, int n) {
        
        ListNode *p = NULL;
        ListNode *p0 = head;
        ListNode *p1 = head;
        ListNode *pre1 = NULL;
        while (--n)        p0 = p0->next;
        
        while (p0->next)
        {
            p0 = p0->next;
            pre1 = p1;
            p1 = p1->next;
        }
        if (p1 == head) p = head->next;
        else
        {
            pre1->next = p1->next;
            p = head;
        }
        return p;
    }
};

LeetCode "Remove Nth Node From End of List",布布扣,bubuko.com

LeetCode "Remove Nth Node From End of List"

原文:http://www.cnblogs.com/tonix/p/3862651.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!