i’m first saving L2 linked list in array first. then i’m reversing L1 and then again i’m iterating over L2 and saving element in second array. Then i’m just comparing first and second array…if the elements are same then i’m increasing cnt variable else breaking the loop…next i’m doing iteration over l2 until k<cnt…
My code is giving run time error and only 1 test case got passed. Why?
you are simpy saving l2 in first and second, so first=second always!
what you can do:
reverse both l1 and l2, then iterate over lists, you have to print last element which is equal.
i’m saving l2 in second after reversing of l1 so both would not be same
what you are doing is
- save l2 in first
- reverse l1
- save l2 in second
just dry run/print the first and second and you will see your mistake
i don’t what is the format for the input as we have to do write only one function in the code
Provide some input to dry run the code
Well to get rid of run error just take size as 10^6 for first and second.
Your code passes all tests then (I’m amazed its a non trivial approach!)
Also an advice try to do it with constant space!
Thanks its working
ok i will try to do in O(1) space
I have a small doubt defining a array like this ->> int* a =new a[10]; i want to ask whether this dynamic array can increase its size or not when gets full ?
whether the size automatically changed like vectors ?
No @mb129162, It won’t.
Dynamically defined array will get space or memory during the run time in the heap.
Dynamic here signifies for when program is executing.
So i have to create new array of bigger size and delete the previous one. Correct me if i’m wrong
Yes @mb129162,
This is exactly what is happening in the case of vectors.
You’ll observe the same when you’ll implement some data structures in your further course.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.