Swap Nodes in Pairs Solutions in C++
Number 24
Difficulty Medium
Acceptance 50.5%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/swap-nodes-in-pairs/// Author : Hao Chen// Date : 2014-06-22/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public:Solution(){srand(time(NULL));}/** Here we have two ways to solve this problem:* 1) keep the list's nodes no change. only swap the data in the list node.* 2) swap the list node physically.*/ListNode *swapPairs(ListNode *head) {if(random()%2){return swapPairs1(head);}return swapPairs2(head);}/*just swap the node's value instead of node*/ListNode *swapPairs1(ListNode *head) {for (ListNode *p = head; p && p->next; p = p->next->next) {int n = p->val;p->val = p->next->val;p->next->val = n;}return head;}/*swap the list nodes physically*/ListNode *swapPairs2(ListNode *head) {ListNode *h = NULL;//using `*p` to traverse the linked listfor (ListNode *p = head; p && p->next; p = p->next) {//`n` is `p`'s next node, and swap `p` and `n` physciallyListNode *n = p->next;p->next = n->next;n->next = p;//using `h` as `p`'s previous nodeif (h){h->next = n;}h=p;//determin the really 'head' pointerif (head == p){head = n;}}return head;}ListNode* swapPairs3(ListNode* head) {// Three pointers point current, previous and next node.ListNode *Curr=head, *Prev=NULL, *Next=NULL;while (Curr && Curr->next ) {Next = Curr->next;//swap nodesCurr->next = Next->next;Prev == NULL ? head = Prev = Next : Prev->next = Next;Next->next = Curr;//set the pointers to next place.Prev = Curr;Curr = Curr->next;}return head;}};
C++ solution by pezy/LeetCode
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/#include <cstddef>struct ListNode {int val;ListNode *next;ListNode(int x) : val(x),next(NULL) {}};class Solution {public:ListNode *swapPairs(ListNode *head) {ListNode *newHead = new ListNode(-1);newHead->next = head;for (ListNode *p1 = newHead, *p2 = head; p2 && p2->next; p1 = p2, p2 = p2->next){p1->next = p2->next;p2->next = p1->next->next;p1->next->next = p2;}return newHead->next;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/swap-nodes-in-pairs/description//// Author : liuyubobobo/// Time : 2017-11-15#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// LinkedList Test Helper FunctionsListNode* createLinkedList(int arr[], int n){if(n == 0)return NULL;ListNode* head = new ListNode(arr[0]);ListNode* curNode = head;for(int i = 1 ; i < n ; i ++){curNode->next = new ListNode(arr[i]);curNode = curNode->next;}return head;}void printLinkedList(ListNode* head){if(head == NULL){cout<<"NULL"<<endl;return;}ListNode* curNode = head;while(curNode != NULL){cout << curNode->val;if(curNode->next != NULL)cout << " -> ";curNode = curNode->next;}cout << endl;return;}void deleteLinkedList(ListNode* head){ListNode* curNode = head;while(curNode != NULL){ListNode* delNode = curNode;curNode = curNode->next;delete delNode;}return;}// Time Complexity: O(n)// Space Complexity: O(1)class Solution {public:ListNode* swapPairs(ListNode* head) {ListNode* dummyHead = new ListNode(0);dummyHead->next = head;ListNode* p = dummyHead;while(p->next && p->next->next){ListNode* node1 = p->next;ListNode* node2 = node1->next;ListNode* next = node2->next;node2->next = node1;node1->next = next;p->next = node2;p = node1;}ListNode* retHead = dummyHead->next;delete dummyHead;return retHead;}};int main() {int arr[] = {1, 2, 3, 4};int n = sizeof(arr) / sizeof(int);ListNode* head = createLinkedList(arr, n);printLinkedList(head);head = Solution().swapPairs(head);printLinkedList(head);deleteLinkedList(head);return 0;}