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

LeetCode 24. Swap Nodes in Pairs

时间:2018-08-12 23:44:28      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:air   else   操作   pairs   next   color   leetcode   lis   null   

考察链表操作。

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head==NULL) return NULL;
        if (head->next==NULL) return head;
        ListNode *preHead=new ListNode(0);
        preHead->next=head;
        ListNode *m=head->next;
        ListNode *t=preHead;
        while(head){
            head->next=m->next;
            m->next=head;
            t->next=m;

            if (head->next)head=head->next;
            else break;
            m=m->next;
            m=m->next;
            if (m->next==NULL) break;
            m=m->next;
            t=t->next;
            t=t->next;
        }
        return preHead->next;
    }
};

 

LeetCode 24. Swap Nodes in Pairs

标签:air   else   操作   pairs   next   color   leetcode   lis   null   

原文地址:https://www.cnblogs.com/travelller/p/9465388.html

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