How to optimize my code ?
Time limit exceeded
Given two sorted Linked List, we need to merge the two Linked List such that the new Linked List still remain sorted.
The Approach is pretty simple that we will Start from the head of the two lists and consume that element from the two of the Lists first which is smaller and add that element into the ans list. After consuming, Advance the iterator to point to the next node in the list. The size of the Lists can be different so after consuming elements simultaneously from the two list, We need to check if any of the two list is empty, and add the elements of that list as it is in the answer List.
you can see this:
if this solves your doubt please mark it as resolved 