class ListNode(var value: Int, var next: ListNode)
object LinkedListUtils {
def findMergePoint(headA: ListNode, headB: ListNode): Option[ListNode] = {
var currA = headA
var currB = headB
while (currA != currB) {
currA = if (currA == null) headB else currA.next
currB = if (currB == null) headA else currB.next
}
Option(currA)
}
}
object Main extends App {
val commonNode = new ListNode(7, null)
val listA = new ListNode(3, new ListNode(1, commonNode))
val listB = new ListNode(5, new ListNode(9, new ListNode(2, commonNode)))
val mergePoint = LinkedListUtils.findMergePoint(listA, listB)
mergePoint match {
case Some(node) => println(s"Merged at node with value ${node.value}")
case None => println("No merge point found")
}
}