Given a linked list, swap every two adjacent nodes and return its head. For example,
Given ->->->, you should return the list as ->->->. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
- Total Accepted: 156137
- Total Submissions: 413794
- Difficulty: Medium
/**
* 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) {
if (head == nullptr || head->next == nullptr) return head;
ListNode dummy(-);
dummy.next = head;
for(ListNode *prev = &dummy, *cur = prev->next, *next = cur->next;
next;
prev = cur, cur = cur->next, next = cur ? cur->next: nullptr) {
prev->next = next;
cur->next = next->next;
next->next = cur;
}
return dummy.next;
}
};
/**
* 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)
{
ListNode dummy(-);
dummy.next = head;
ListNode *p = &dummy, *q = head;
while (q && q->next)
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
p = q;
q = q->next;
}
return dummy.next;
}
};
3m 37.45%
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory is allowed. For example,
Given this linked list: ->->->-> For k = , you should return: ->->->-> For k = , you should return: ->->->->
- Total Accepted: 89044
- Total Submissions: 294580
- Difficulty: Hard
解题思路:节点指针的指向变换比较麻烦.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution { //by guxuanqing@gmail.com
public:
ListNode* reverseKGroup(ListNode* head, int k) {
int len = ;
ListNode dummy(-);
dummy.next = head;
if(k == ) return dummy.next;
ListNode *p = &dummy, *prevq = &dummy, *q = head;
while (p->next) {
p = p->next;
++len;
}
p = &dummy;
div_t dv = div(len, k);
int shang = dv.quot; while (shang--)
{
ListNode *tmpp = q;
prevq = q;
q = q->next;
int tmpk = k - ;
while (tmpk--)
{
ListNode *qq = q->next;
q->next = prevq;
prevq = q;
q = qq;
}
tmpp->next = q;
p->next = prevq;
p = tmpp;
prevq = tmpp;
}
return dummy.next;
}
};
26ms 31.72%
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if (head == nullptr || head->next == nullptr || k < ) return head;
ListNode dummy(-);
dummy.next = head;
for(ListNode *prev = &dummy, *end = head; end; end = prev->next) {
for (int i = ; i < k && end; i++)
end = end->next;
if (end == nullptr) break;
prev = reverse(prev, prev->next, end);
}
return dummy.next;
}
ListNode* reverse(ListNode *prev, ListNode *begin, ListNode *end) {
ListNode *end_next = end->next;
for (ListNode *p = begin, *cur = p->next, *next = cur->next;
cur != end_next;
p = cur, cur = next, next = next ? next->next : nullptr) {
cur->next = p;
}
begin->next = end_next;
prev->next = end;
return begin;
}
};
26ms