From 13e1e805a627c44623f5e12dd941837c3f533af7 Mon Sep 17 00:00:00 2001 From: anebz Date: Fri, 9 Aug 2019 00:03:49 +0200 Subject: [PATCH] Updated exercises and README with ch2 --- 02. Linked lists/2.7. intersection.java | 33 ++++++++++++++++++++++--- 02. Linked lists/2.7. intersection.md | 4 +++ README.md | 5 ++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/02. Linked lists/2.7. intersection.java b/02. Linked lists/2.7. intersection.java index 274695f..67383dd 100644 --- a/02. Linked lists/2.7. intersection.java +++ b/02. Linked lists/2.7. intersection.java @@ -4,7 +4,8 @@ public static LinkedListNode intersect(LinkedListNode a, LinkedListNode b){ LinkedListNode a_head = a; LinkedListNode b_head = b; - int a_len, b_len = 1; + int a_len = 1; + int b_len = 1; // iterate through A while (a_head.next != null){ @@ -22,16 +23,16 @@ public static LinkedListNode intersect(LinkedListNode a, LinkedListNode b){ if (a_head != b_head) return null; // changed from before // compare lengths, adjust node - if (a_len == 1 or b_len == 1) return a_head; + if (a_len == 1 || b_len == 1) return a_head; // improved LinkedListNode shorter = a_len < b_len ? a : b; LinkedListNode longer = b_len < a_len ? b : a; int diff = Math.abs(a_len - b_len); - while(k > 0 && longer != null){ + while(diff > 0 && longer != null){ longer = longer.next; - k--; + diff--; } // there's intersection @@ -41,4 +42,28 @@ public static LinkedListNode intersect(LinkedListNode a, LinkedListNode b){ } return shorter; +} + +// https://stackoverflow.com/questions/1594061/check-if-two-linked-lists-merge-if-so-where/14956113#14956113 +static int FindMergeNode(LinkedListNode headA, LinkedListNode headB) { + LinkedListNode currentA = headA; + LinkedListNode currentB = headB; + + // Do till the two nodes are the same + while (currentA != currentB) { + // If you reached the end of one list start at the beginning of the other one + // currentA + if (currentA.next == null) { + currentA = headB; + } else { + currentA = currentA.next; + } + // currentB + if (currentB.next == null) { + currentB = headA; + } else { + currentB = currentB.next; + } + } + return currentB.data; } \ No newline at end of file diff --git a/02. Linked lists/2.7. intersection.md b/02. Linked lists/2.7. intersection.md index 2d0451b..ccf285a 100644 --- a/02. Linked lists/2.7. intersection.md +++ b/02. Linked lists/2.7. intersection.md @@ -250,3 +250,7 @@ Takeaways: * Get a new class for the tail and lengths Improved algorithm in **2.7. intersection.java**. Because we're comparing by reference not value, the condition in `while` won't be met if two nodes have the same `.data` but different `.next`. + +## Update + +New solution found [at stackoverflow](https://stackoverflow.com/questions/1594061/check-if-two-linked-lists-merge-if-so-where/14956113#14956113), without any pointers. diff --git a/README.md b/README.md index 0654013..2765989 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,11 @@ * Partition list * Sum lists * Check if palindrome +* List intersection +* Loop detection +* Insert node at beginning +* Insert node at end +* Reverse list, single and double linked ## Chapter 7 Object-oriented design