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) {
- if(!head) return head;
- ListNode *dummy = new ListNode(0, head);
- ListNode *curr, *next, *prev;
- curr = head; next = curr->next; prev = dummy;
- while(next){
- curr->next = next->next;
- next->next = curr;
- prev->next = next;
- prev = curr;
- curr = curr->next;
- next = (curr) ? curr->next : NULL;
- }
- return dummy->next;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment