Page 534 - Computer science 868 Class 12
P. 534
break;
ptr=ptr.next;
}
if(ptr = = null) // if not found
System.out.print("Number not found in linked list");
}
10. Write an algorithm/Java code to concatenate two existing linked lists using method: void concat(Node startA,
Node startB) where startA and startB refer to the start pointers of the two linked lists.
start-A 1001
29 1098 18 1156 35 NULL 3001
1001 1098 1156
start-B 3001 NULL
29 3027 18 3356 35 NULL
3001 3027 3356
Ans. Algorithm
Step 1: Start.
Step 2: Create a new Node ptr and initialise it to startA
Step 3: Repeat Step 4 while ptr.next != null
Step 4: Assign ptr = ptr.next
Step 6: Set ptr.next= startB
Step 7: Set startB=null
Step 8: Stop.
Java Code
void concat(Node startA , Node startB)
{
Node ptr=new Node()
ptr = startA; // initialise prt to start
while(ptr.next !=null) // end of linked list
{
ptr=ptr.next;
}
ptr.next = startB // merging the two linked lists by assigning startB to the
last node
startB=null
}
532532 Touchpad Computer Science-XII

