Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following two linked lists do _not_ intersect at all because while the node

`linked_list_b: 1 -> 2 -> 5 -> 4 -> 3`

Thereare no cycles anywhere in the linked list structures. Assume any intersection includes the tails of each list.
There are no cycles anywhere in the linked list structures. Assume any intersection includes the tails of each list.


The linked lists must retain their original structure after the function returns.
Expand Down
32 changes: 31 additions & 1 deletion linked_lists/intersection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@



from email.errors import HeaderParseError


class Node:
def __init__(self, value):
self.val = value
Expand All @@ -9,6 +12,33 @@ def __init__(self, value):

def intersection_node(headA, headB):
""" Will return the node at which the two lists intersect.
--> pointing TO THE SAME REFERENCE IN MEMORY
If the two linked lists have no intersection at all, return None.
"""
pass
# ''' create an empty variable to maintain the first seqential digit'''
first = None
current_a = headA
current_b = headB

while headA and headB:
Copy link
Contributor

@char-adadev char-adadev Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super close to getting all the tests to pass, great job!!

For getting that last test to pass, I would take a look at a couple of things:

  • Try running without the outer loop while headA and headB. Is there any difference? You might not need this loop since you're not advancing the pointers for headA and headB.
  • Due to your if, elif, else statement at the bottom of the while headA and headB loop, the loop may just be running one time before returning
  • I would recommend walking through your solution with two small example lists like:

1 -> 2 -> 3
2 -> 3

Do you run into any null pointers while walking through your code?

You may notice that your solution is erroring out on line 32 or line 33 for differing length lists. It seems as if the pointers are advancing one-by-one without making a double check to see if one list has reached the end and resetting the pointer (which you do have a check for in lines 25 through 33!).

I was able to re-work your solution very slightly to get all the tests to pass from my side:

first = None
current_a = headA
current_b = headB

while current_a != current_b:
    if current_a is None and current_b is not None:
        current_a = headA
        current_b = current_b.next
    elif current_b is None and current_a is not None:
        current_b = headB
        current_a = current_a.next
    else:
        current_a = current_a.next
        current_b = current_b.next

if current_a == current_b:
    first = current_a
    return first

elif current_a != current_b:
    return first
else:
    return first

Overall, you did a great job and I can tell you put a lot of effort into this!

# '''check to see if current_a is equal to current_b:'''
if current_a is None and current_b is not None:
current_a = headA
current_b = current_b.next
if current_b is None and current_a is not None:
current_b = headB
current_a = current_a.next
while current_a != current_b:
current_a = current_a.next
current_b = current_b.next
# return first
# '''set the empty variable to first same node value'''
if current_a == current_b:
first = current_a
return first

elif current_a != current_b:
return first
else:
return first

30 changes: 29 additions & 1 deletion tests/test_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,32 @@ def test_will_return_none_nodes_have_same_value_different_reference():
answer = intersection_node(head_a, head_b)

#Assert
assert answer is None
assert answer is None


## create a test case to chek if the end of the linked lists are the same value
'''Assume any intersection includes the tails of each list.'''

def test_will_return_tail_if_tail_intersection():
#arrange
node_d1 = Node("D")
node_q1 = Node("Q")
node_f1 = Node("F")

node_c1 = Node("C")
node_s1 = Node("S")
node_f1 = Node("F")
#List A: [d, q, f1]
node_d1.next = node_q1
node_q1.next = node_f1

#List B: [c, s, f1]
node_c1.next = node_s1
node_s1.next = node_f1

head_a = node_d1
head_b = node_c1
#act
answer = intersection_node(head_a, head_b)
#assert
assert answer == node_f1