-
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.
Update Day06/Intersection of Two Linked Lists.cpp, Day06/Palindrome L…
…inked List.cpp, Day06/Find the starting point of the Loop of LinkedList.cpp, Day06/Flattening a Linked List.cpp, Day06/Reverse Nodes in k-Group.cpp, Day06/README.md
- Loading branch information
1 parent
b4c8242
commit 698abf9
Showing
6 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Day06/Find the starting point of the Loop of LinkedList.cpp
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,27 @@ | ||
ListNode *detectCycle(ListNode *head) { | ||
ListNode* slow = head; | ||
ListNode* fast = head; | ||
|
||
bool flag = false; | ||
|
||
while(fast and fast -> next and fast -> next -> next) { | ||
slow = slow -> next; | ||
fast = fast -> next -> next; | ||
if(slow == fast) { | ||
flag = true; | ||
break; | ||
} | ||
} | ||
|
||
if(!flag) { | ||
return nullptr; | ||
} | ||
|
||
slow = head; | ||
|
||
while(slow != fast) { | ||
slow = slow -> next; | ||
fast = fast -> next; | ||
} | ||
return slow; | ||
} |
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,38 @@ | ||
Node *merge(Node* rootA, Node* rootB) { | ||
Node* temp = new Node(0); | ||
Node* ans = temp; | ||
|
||
while(rootA and rootB) { | ||
if(rootA -> data < rootB -> data) { | ||
temp -> bottom = rootA; | ||
rootA = rootA -> bottom; | ||
} else { | ||
temp -> bottom = rootB; | ||
rootB = rootB -> bottom; | ||
} | ||
temp = temp -> bottom; | ||
} | ||
|
||
if(rootA) { | ||
temp -> bottom = rootA; | ||
} | ||
if(rootB) { | ||
temp -> bottom = rootB; | ||
} | ||
|
||
return ans -> bottom; | ||
} | ||
|
||
Node *flatten(Node *root) | ||
{ | ||
// Your code here | ||
if(!root or !root -> next) { | ||
return root; | ||
} | ||
|
||
root -> next = flatten(root -> next); | ||
|
||
root = merge(root, root -> next); | ||
|
||
return root; | ||
} |
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,17 @@ | ||
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { | ||
ListNode* currA = headA; | ||
ListNode* currB = headB; | ||
|
||
while(true) { | ||
if(currA == currB) { | ||
return currA; | ||
} else if (!currA) { | ||
currA = headA; | ||
} else if (!currB) { | ||
currB = headB; | ||
} else { | ||
currA = currA -> next; | ||
currB = currB -> 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
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
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,25 @@ | ||
ListNode* reverseKGroup(ListNode* head, int k) { | ||
ListNode* curr = head; | ||
for(int i = 0; i < k; i++) { | ||
if(!curr) { | ||
return head; | ||
} | ||
curr = curr -> next; | ||
} | ||
|
||
ListNode* prev = nullptr; | ||
ListNode* temp = nullptr; | ||
curr = head; | ||
|
||
for(int i = 0; i < k; i++) { | ||
temp = curr -> next; | ||
curr -> next = prev; | ||
prev = curr; | ||
curr = temp; | ||
} | ||
|
||
if(curr) { | ||
head -> next = reverseKGroup(curr, k); | ||
} | ||
return prev; | ||
} |