Swap Nodes in Pairs
https://leetcode.com/problems/swap-nodes-in-pairs/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head || !head->next) return head;
ListNode* temp;
temp = head->next;
head->next = swapPairs(head->next->next);
temp->next = head;
return temp;
}
};
// We use a dummy head node to make handling head operations simpler
ListNode *dummy = new ListNode(-1), *tail = dummy;
// add the dummy node to list
tail->next = head;
while(head && head->next) {
ListNode *nextptr = head->next->next;
// swap the adjacent nodes
// 2nd node comes to 1st pos
tail->next = head->next;
// connecting 2nd node to 1st node
(head->next)->next = head;
// make the 1st node connected to next node on list
tail = head;
tail->next = nextptr;
head = nextptr;
}
head = dummy->next;
delete dummy;
return head;
Comments
Post a Comment