Skip to content

Commit

Permalink
return from haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryAIwei committed Feb 18, 2020
1 parent 5a88e3b commit 888dba2
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
78 changes: 78 additions & 0 deletions answer/25._ReverseNodesink-Group.md
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;
}
};
```

53 changes: 53 additions & 0 deletions answer/25._Reverse_Nodes_in_k-Group.md
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;
}
}
};
```

0 comments on commit 888dba2

Please sign in to comment.