文章目录
problem Ⅰ
143. Reorder List
You are given the head of a singly linked-list. The list can be represented as:
L0 → L1 → … → Ln - 1 → Ln
Reorder the list to be on the following form:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
You may not modify the values in the list’s nodes. Only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Example 2:
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
my solution
/**
* 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:
void reorderList(ListNode* head) {
if(!head || !head->next || !head->next->next)return;
stack<ListNode*> stk;
ListNode *tmp1 = head;
while(tmp1){
stk.push(tmp1);
tmp1 = tmp1->next;
}
ListNode *tmp2 = head;
int size = stk.size();
for(int i=0; i<size/2; i++){
ListNode *tops = stk.top();
stk.pop();
tops->next = tmp2->next;
tmp2->next = tops;
tmp2 = tmp2->next->next;
}
tmp2->next = NULL;
}
};
problem Ⅱ
25. Reverse Nodes in k-Group
Given the head of a linked list, reverse the nodes of the list k at a time, and return the 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 list’s nodes, only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
my solution wrong
/**
* 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* reverseKGroup(ListNode* head, int k) {
ListNode *heads=head, *tail=head;
ListNode *tmps = new ListNode();
tmps->next = head;
ListNode *tmp = tmps;
ListNode *tt;
int cnt = 1;
while(tail){
if(cnt%k==0){
tail = tail->next;
ListNode* prev = tail, *nextNode = NULL, *curr = heads;
while(curr != tail){
nextNode = curr->next;
curr->next = prev;
prev = curr;
if(curr->next = tail)tt = curr;
curr = nextNode;
}
heads = tail;
cnt++;
tmp->next = prev;
tmp = tt;
}else{
tail = tail->next;
cnt++;
}
}
return tmps->next;
}
};
my solution 1 recursive
/**
* 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* reverseKGroup(ListNode* head, int k) {
ListNode *tmp = head;
for(int i=0; i<k; i++){
if(!tmp)return head;
tmp = tmp->next;
}
ListNode *prev = NULL, *nxt = NULL, *curr = head;
for(int i=0; i<k; i++){
nxt = curr->next;
curr->next = prev;
prev = curr;
curr = nxt;
}
head->next = reverseKGroup(curr, k);
return prev;
}
};
my solution 2 iterative
/**
* 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* reverseKGroup(ListNode* head, int k) {
ListNode *dummy = new ListNode();
dummy->next = head;
ListNode *before = dummy, *after = head;
ListNode *curr, *nxt, *prev;
while(true){
ListNode *tmp = after;
for(int i=0; i<k; i++){
if(!tmp)return dummy->next;
tmp = tmp->next;
}
prev = before;
curr = after;
for(int i=0; i<k; i++){
nxt = curr->next;
curr->next = prev;
prev = curr;
curr = nxt;
}
after->next = curr;
before->next = prev;
before = after;
after = curr;
}
}
};
NOTE:
my thought is like the illustration below