Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- ListNode* swapPairs(ListNode* head) {
- ListNode* dummy = new ListNode();
- dummy->next = head;
- ListNode* prev = dummy;
- ListNode* curr = head;
- ListNode* nextpair;
- ListNode* seccurr;
- while(curr!=nullptr && curr->next!=nullptr)
- {
- nextpair = curr->next->next;
- seccurr = curr->next;
- seccurr->next = curr;
- curr->next = nextpair;
- prev->next = seccurr;
- prev = curr;
- curr = nextpair;
- }
- return dummy->next;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment