Skip to content

Commit

Permalink
Update Day06/Intersection of Two Linked Lists.cpp, Day06/Palindrome L…
Browse files Browse the repository at this point in the history
…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
muditmahajan21 committed May 13, 2022
1 parent b4c8242 commit 698abf9
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 2 deletions.
27 changes: 27 additions & 0 deletions Day06/Find the starting point of the Loop of LinkedList.cpp
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;
}
38 changes: 38 additions & 0 deletions Day06/Flattening a Linked List.cpp
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;
}
17 changes: 17 additions & 0 deletions Day06/Intersection of Two Linked Lists.cpp
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;
}
}
}
1 change: 0 additions & 1 deletion Day06/Palindrome Linked List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ bool isPalindrome(ListNode* head) {
ListNode* dummy = head;

while(slow) {
cout << slow -> val << " ";
if(dummy -> val != slow -> val) return false;
dummy = dummy -> next;
slow = slow -> next;
Expand Down
50 changes: 49 additions & 1 deletion Day06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Link: [https://leetcode.com/problems/linked-list-cycle/]
- Initialize slow and fast pointers to head.
- While fast -> next and fast -> next -> next is not null.
- Run slow = slow -> next and fast = fast -> next -> next.
- If slow is equal to slow, return true.
- If slow is equal to fast, return true.
- Outside the loop, return false.

Time Complexity: O(n).
Expand All @@ -25,3 +25,51 @@ Link: [https://leetcode.com/problems/palindrome-linked-list/]
- Else, at the end of the ;loop, return true.

Time Complexity: O(n)

## Intersection of Two Linked Lists

Link: [https://leetcode.com/problems/intersection-of-two-linked-lists/]

- Run an infinite loop.
- Run the loop till the traversing nodes of both lists are not equal.
- If one is null, just re-initialize to the head.

Time Complexity: O(2 * max(lenA, lenB))

## Find the starting point of the Loop of LinkedList

Link: [https://leetcode.com/problems/linked-list-cycle-ii/]

- Initialize two pointers slow and fast to head.
- While fast and fast -> next are not null, run the while loop while they are not equal and fast or fast -> next is not null.
- If they become null, return null. Else
- Re-initialize slow to head and run another loop.
- This time, the loop is to be run while slow is not equal to fast.
- When, they become equal, return any one of them.

Time Complexity: O(n)

## Flattening a Linked List

Link: [https://practice.geeksforgeeks.org/problems/flattening-a-linked-list/1]

### Approach: Merge Sort

- Use recursion for root and root -> next.
- Use a separate merge function to merge the bottom linked lists of root and root -> next.
- In the merge function, create a new temp node and while both are not null, keep adding the elements one by one from the two linked lists.
- If one is not null, add that one at the end,
- Return the new bottom linked list and assign it to root.

Time Complexity: O(n)

## Reverse Nodes in k-Group

Link: [https://leetcode.com/problems/reverse-nodes-in-k-group/]

- Run a loop of i less than k, if the current node is null, return head.
- Run another loop for i less than k and reverse the linked list using temp and prev nodes.
- If the current node is not null, then recurse on head -> next to the current node and k.
- Return the prev node at the end as the answer.

Time Complexity: O(n)
25 changes: 25 additions & 0 deletions Day06/Reverse Nodes in k-Group.cpp
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;
}

0 comments on commit 698abf9

Please sign in to comment.