Problem:
Solution:
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (head == NULL )
return NULL;
if (head->next == NULL)
return head;
ListNode *temp = NULL, *temp2 = NULL, *temp3 = NULL, *newHead = NULL, *prev = NULL;
temp = head;
newHead = head->next;
while (temp != NULL) {
temp3 = temp;
temp2 = temp->next;
if (temp2 != NULL) {
temp->next = temp2->next;
temp2->next = temp3;
if (prev != NULL)
prev->next = temp2;
}
prev = temp3;
temp = temp->next;
}
return newHead;
}
};
=========================
/**
* 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) {
// Recursive. ============================
/*
if (head == NULL || head -> next == NULL) {
return head;
} else {
ListNode *tmp = head -> next;
head -> next = swapPairs(head -> next -> next);
tmp -> next = head;
return tmp;
}
*/
// Iterative. ===============================
if (head == NULL || head -> next == NULL) {
return head;
}
ListNode *curr = head;
ListNode *ans = NULL, *prev = NULL, *next = NULL;
while (curr != NULL && curr -> next != NULL) {
if (prev == NULL) {
ans = curr -> next;
} else {
prev -> next = curr -> next;
}
prev = curr;
next = curr -> next -> next;
curr -> next -> next = curr;
curr = next;
}
if (curr != NULL && curr -> next == NULL) {
prev -> next = curr;
} else {
prev -> next = NULL;
}
return ans;
}
};
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given
Given
1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
Solution:
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (head == NULL )
return NULL;
if (head->next == NULL)
return head;
ListNode *temp = NULL, *temp2 = NULL, *temp3 = NULL, *newHead = NULL, *prev = NULL;
temp = head;
newHead = head->next;
while (temp != NULL) {
temp3 = temp;
temp2 = temp->next;
if (temp2 != NULL) {
temp->next = temp2->next;
temp2->next = temp3;
if (prev != NULL)
prev->next = temp2;
}
prev = temp3;
temp = temp->next;
}
return newHead;
}
};
=========================
/**
* 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) {
// Recursive. ============================
/*
if (head == NULL || head -> next == NULL) {
return head;
} else {
ListNode *tmp = head -> next;
head -> next = swapPairs(head -> next -> next);
tmp -> next = head;
return tmp;
}
*/
// Iterative. ===============================
if (head == NULL || head -> next == NULL) {
return head;
}
ListNode *curr = head;
ListNode *ans = NULL, *prev = NULL, *next = NULL;
while (curr != NULL && curr -> next != NULL) {
if (prev == NULL) {
ans = curr -> next;
} else {
prev -> next = curr -> next;
}
prev = curr;
next = curr -> next -> next;
curr -> next -> next = curr;
curr = next;
}
if (curr != NULL && curr -> next == NULL) {
prev -> next = curr;
} else {
prev -> next = NULL;
}
return ans;
}
};
No comments:
Post a Comment