Swap values in Paris - c++


  • 0
    L
    /**
     * 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* p=head;
            ListNode* q=head;
            int i=0;
            while(q)
            {
                p=q;
                q=q->next;
                if(q==NULL) break;
                if(i==0)
                {
                    int val=p->val;
                    p->val=q->val;
                    q->val=val;
                    
                    i++;
                }
                else
                {
                    i=0;
                }
            }
            return head;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.