LeetCode Swap Nodes in Pairs

时间:2014-06-05 12:55:24   收藏:0   阅读:216
bubuko.com,布布扣
class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        ListNode *a = NULL;
        ListNode *b = NULL;
        ListNode *tail = NULL;
        ListNode *nhead = NULL;
        a = head;
        while (a != NULL && (b = a->next) != NULL) {
            a->next = b->next;
            b->next = a;
            if (tail != NULL) {
                tail->next = b;
            } else {
                nhead = b;
            }
            tail = a;
            a = a->next;
        }
        if (nhead == NULL) {
            nhead = head;
        }
        return nhead;
    }
};
bubuko.com,布布扣

准备六级,水一发

LeetCode Swap Nodes in Pairs,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!