首页 > 其他 > 详细

24:Swap Nodes in Pairs【链表】

时间:2015-05-20 17:56:24      阅读:145      评论:0      收藏:0      [点我收藏+]

题目链接:https://leetcode.com/problems/swap-nodes-in-pairs/

/*题意:将链表相邻的两两结点交换*/

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head == NULL || head->next == NULL) return head;
        ListNode *pre = NULL;
        ListNode *Next = head;

        while(Next && Next->next) {
            ListNode *first = Next;
            ListNode *second = Next->next;
            Next = second->next;
            if(pre) {
                pre->next = second;
                second->next = first;
                first->next = Next;
                pre = first;
            }
            else {
                head = second;
                second->next = first;
                first->next = Next;
                pre = first;
            }
        }
        return head;
    }
};

  

24:Swap Nodes in Pairs【链表】

原文:http://www.cnblogs.com/jzmzy/p/4517646.html

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