In: Computer Science
Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the basic list operations. Pseudo-code is acceptable.
SOURCE CODE
ListIterator l1 = L1.iterator();
ListIterator l2 = L2.iterator();
int i = 0;
if(l1.hasNext() && l2.hasNext())
{
item1 = l1.next();
item2 = l2.next();
}
while(item1 != null && item2 != null)
{
res = item1.compareTo(item2);
if(res == 0)
{
L3.add(item1)
item1 =
(l1.hasNext())? l1.next() : null;
item2 =
(l2.hasNext())? l2.next() : null;
}
else if(res > 0)
{
L3.add(item2);
item2 =
(l2.hasNext())? l2.next() : null;
}
else
{
L3.add(item1);
item1 =
(l1.hasNext())? l1.next() : null;
}
}
SCREENSHOT
please give a upvote if u feel helpful.