Second Freebie!!! This one is marked as "easy"
This problem was asked by Google.
Given two singly linked lists that intersect at some point, find the intersecting node. The lists are non-cyclical.
For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.
In this example, assume nodes with the same value are the exact same node objects.
Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.
Okay, so my solution didn't meet the O(n+m) complexity. But Iound the correct algorithm, and both are in place and the code is updated. @freemo this was kind of interesting, reminds me of an old card trick I used to do.
@Absinthe Are you sure you interpreted the problem correctly? It seems your solution only works with non-intersecting lists that just happen to have the same numbers at the end.
For instance your solution breaks if I change the beginning of main() to the following:
common = Node(8,Node(10));
mlist = LList(Node(3,Node(7,common)))
nlist = LList(Node(99,Node(1,common)))
@khird take another look. I removed the reversing one, and replaced it with one that destroys the next pointers, and and another that modifies the vals. Along with the 2 pointer one, these should all work. I think it needs some cleanup though. Let me know what you see.
I spoke with a colleague over lunch today and we got into solutions for finding cyclical linked lists. So taking from the discussion we had, I wanted to see if I could implement it. Unfortunately, none of these except the bad list reversal one are my own design. The two pointer solution was suggested in a similar problem on LeetCode. So basically, I wanted to implement it and see it work. I just had a few minutes between work and the gym, so I wanted to get something up. Definitely not grade A code.
Ultimately the two pointer solution is likely what they were looking for. Certainly can use some code cleanup. At least some error checking. :)
Interesting, that interleaving of the if/else and the while loop is very strange to my eye but it's cool that Python allows it.
The nondestructive one looks solid now; the destructive one still claims to find an intersection in disjoint lists though.
@Absinthe
The code isn't that bad, you just have to be careful you don't go down a dead end with your algorithm. It's much easier to add error checking etc. at the end if you designed a versatile solution than it is to try and generalise one of the more brittle ones.
Would you mind if I suggested a challenge? I have one involving graph manipulation, which is kind of up @freemo's alley.