-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a88e3b
commit 888dba2
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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. | ||
|
||
Example: | ||
|
||
Given this linked list: 1->2->3->4->5 | ||
|
||
For k = 2, you should return: 2->1->4->3->5 | ||
|
||
For k = 3, you should return: 3->2->1->4->5 | ||
|
||
Note: | ||
|
||
Only constant extra memory is allowed. | ||
You may not alter the values in the list's nodes, only nodes itself may be changed | ||
|
||
|
||
source:LeetCode https://leetcode-cn.com/problems/reverse-nodes-in-k-group/ | ||
*** | ||
|
||
Diveide and Conquer | ||
|
||
time complex: O(n) | ||
|
||
space complex: O(1) | ||
|
||
``` | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode(int x) : val(x), next(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* reverseK(ListNode* head, int k){ | ||
if(head==nullptr) return nullptr; | ||
else{ | ||
ListNode* tail = head; | ||
for(int i=0;i<k;++i){ | ||
if(tail==nullptr) return head; | ||
else{ | ||
tail = tail->next; | ||
} | ||
} | ||
ListNode* lastOne = tail; | ||
ListNode* thisOne = head; | ||
ListNode* nextOne = head->next; | ||
for(int i = 0; i<k;++i){ | ||
nextOne = thisOne->next; | ||
thisOne->next = lastOne; | ||
lastOne = thisOne; | ||
thisOne = nextOne; | ||
} | ||
return lastOne; | ||
} | ||
} | ||
ListNode* reverseKGroup(ListNode* head, int k) { | ||
ListNode* dummy = new ListNode(0); | ||
dummy->next = head; | ||
ListNode* lastTail = dummy; | ||
while(lastTail!=nullptr){ | ||
lastTail->next = reverseK(lastTail->next,k); | ||
//go to the new tail | ||
for(int i=0;i<k;i++){ | ||
if(lastTail==nullptr) break; | ||
else lastTail = lastTail->next; | ||
} | ||
} | ||
return dummy->next; | ||
} | ||
}; | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Given a linked list, swap every two adjacent nodes and return its head. | ||
|
||
You may not modify the values in the list's nodes, only nodes itself may be changed. | ||
|
||
|
||
|
||
Example: | ||
|
||
Given 1->2->3->4, you should return the list as 2->1->4->3. | ||
|
||
|
||
source:LeetCode https://leetcode-cn.com/problems/swap-nodes-in-pairs/ | ||
*** | ||
|
||
Diveide and Conquer | ||
|
||
time complex: O(n) | ||
|
||
space complex: O(1) | ||
|
||
``` | ||
/** | ||
* 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==NULL) return head; | ||
else if(head->next==NULL) return head; | ||
else{ | ||
ListNode* tempNode = head; | ||
head = head->next; | ||
tempNode->next = head->next; | ||
head->next = tempNode; | ||
ListNode* lastTail = head->next; | ||
while(lastTail->next != NULL && lastTail->next->next != NULL){ | ||
tempNode = lastTail->next; | ||
lastTail->next = tempNode->next; | ||
tempNode->next = lastTail->next->next; | ||
lastTail->next->next = tempNode; | ||
lastTail = lastTail->next->next; | ||
} | ||
return head; | ||
} | ||
} | ||
}; | ||
``` | ||
|