Two Singly linked Lists, size m , r and want to insert the first linked list nodes after the head of the second linked list, and the time complexity has to be O(1) of the method.
This really an intereseting difficult problem for me. Eatch time I think of a solution, the Time complexity is O(m+r)
I need some hints to solve this. I consumed useless effort on this problem.
EDIT:
Let me share what I have so far:
- Create a new Linked List
- Add the HEAD of the 2nd list
- Still O(1)
- Add all the nodes of 1st list
- Becomes (n)
-
Add the rest of the nodes from the 1st list
-
Becomes another (n-1)
UPDATE:
What do you think about this? I got inspired directly after I asked here 🙂

Assuming you have these structures:
Reminder to self: the goal is: “insert the first linked list nodes after the head of the second linked list”.
Then all you’ve got to do is:
List2 still ends where it did before (its tail node is the same).
You’ve now got
list1with a “floating” head, which is generally bad news… but if you iterate overlist1you’ll get all the elements from both original lists…