Union of Two Lists
Merge two lists into a new list containing all unique elements.
List A
10
0xu1
20
0xu2
30
0xu3
NULL
List B
20
0xv1
30
0xv2
40
0xv3
NULL
Status: Click 'Compute Union' to start.
Speed:
1function getUnion(headA, headB):
2 unionSet = new Set()
3 result = new LinkedList()
4
5 for node in [headA, headB]:
6 while node:
7 if !unionSet.has(node.value):
8 unionSet.add(node.value)
9 result.add(node.value)
10 node = node.next
11
12 return result.head