码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Remove Nth Node From End of List

时间:2014-07-18 18:24:55      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   re   

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

一次过呗

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

LeetCode Remove Nth Node From End of List

标签:style   blog   color   io   for   re   

原文地址:http://www.cnblogs.com/lailailai/p/3853462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!