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

24:Swap Nodes in Pairs【链表】

时间:2015-05-20 17:57:06      阅读:112      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!