标签:int ons node public div val pac const swap
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given1->2->3->4, you should return the list as2->1->4->3.
Note:
AC code:
/**
 * 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 **dummy = &head, *a, *b;
        while ((a=*dummy) && (b=a->next)) {
            a->next = b->next;
            b->next = a;
            *dummy = b;
            dummy = &(a->next);
        }
        return head;
    }
};
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Swap Nodes in Pairs.
1 2 3 4 5 6 a b 3 1 *pp pp->2->1-> a b (b)(a) 4 3 *pp *pp pp->4->3-> a b (b)(a) 5 4 *pp *pp pp->6->5 a b (b)(a) 6 5 *pp *pp
标签:int ons node public div val pac const swap
原文地址:https://www.cnblogs.com/ruruozhenhao/p/9746036.html