Complete the function joinLinkedLists() to take in two nodes that represent the
ID: 3719937 • Letter: C
Question
Complete the function joinLinkedLists() to take in two nodes that represent the beginning of two Linked Lists, n1 and n2, and join these Linked Lists such that n2 is attached to the end of n1.
Example
n1: 100 -> 85 -> 75 -> 65
n2: 23 -> 22 -> 12 -> 10
joinLinkedLists(n1, n2)
# returns 100 -> 85 -> 75 -> 65 -> 23 -> 22 -> 12 -> 10
class Node:
def __init__(self, value):
self.value = value
self.next = None
def joinLinkedLists(n1, n2):
# TODO
?
Explanation / Answer
class Node: def __init__(self, value): self.value = value self.next = None def joinLinkedLists(n1, n2): temp = n1 while temp.next is not None: temp = temp.next temp.next = n2