I have written a function that merges two linked list. (Note that the function is based on pre-given code in case you wonder why i'm calling a function node(i)).
public SLL mergeUnsorted(SLL otherList)
{
// find length of this list
int length = 0 ;
Iterator itr = this.iterator() ;
while (itr.hasNext())
{
Object elem = itr.next() ;
length++ ;
}
// get each node from this list and
// add it to front of otherList list
int i = length -1 ;
while (i >= 0)
{
// returns node from this list
SLLNode ins = node(i) ;
ins.succ = otherList.first ;
otherList.first = ins ;
i-- ;
}
return this ;
}
first part O(n) second part O(n)
overall complexity O(n)
or is it O(n^2) because i traverse the list twice?