LeetCode 24 Swap Nodes in Pairs

题目

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        
        ListNode* res = head;
        ListNode* term = res;
        while(head!=NULL)
        {
            if(head->next!=NULL)
            {
                ListNode* temp = head->next;
                
                head->next = temp->next;
                temp->next = head;
                term->next = temp;
                
                 term=head;
                head=head->next;
              
            }
            else
                break;
        }
        return res;
    }
};
posted @ 2019-07-04 14:26  Shendu.CC  阅读(81)  评论(0编辑  收藏  举报