Updated exercises and README with ch2

This commit is contained in:
anebz 2019-08-09 00:03:49 +02:00
parent 93e084ce48
commit 13e1e805a6
3 changed files with 38 additions and 4 deletions

View File

@ -4,7 +4,8 @@ public static LinkedListNode intersect(LinkedListNode a, LinkedListNode b){
LinkedListNode a_head = a; LinkedListNode a_head = a;
LinkedListNode b_head = b; LinkedListNode b_head = b;
int a_len, b_len = 1; int a_len = 1;
int b_len = 1;
// iterate through A // iterate through A
while (a_head.next != null){ 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 if (a_head != b_head) return null; // changed from before
// compare lengths, adjust node // 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 // improved
LinkedListNode shorter = a_len < b_len ? a : b; LinkedListNode shorter = a_len < b_len ? a : b;
LinkedListNode longer = b_len < a_len ? b : a; LinkedListNode longer = b_len < a_len ? b : a;
int diff = Math.abs(a_len - b_len); int diff = Math.abs(a_len - b_len);
while(k > 0 && longer != null){ while(diff > 0 && longer != null){
longer = longer.next; longer = longer.next;
k--; diff--;
} }
// there's intersection // there's intersection
@ -42,3 +43,27 @@ public static LinkedListNode intersect(LinkedListNode a, LinkedListNode b){
return shorter; 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;
}

View File

@ -250,3 +250,7 @@ Takeaways:
* Get a new class for the tail and lengths * 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`. 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.

View File

@ -26,6 +26,11 @@
* Partition list * Partition list
* Sum lists * Sum lists
* Check if palindrome * 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 ## Chapter 7 Object-oriented design