I am confused with pointer and linked list since I tounched computer as freshman in university.
just verify solution of other guys.
Java
Iterative
© LeetCode – Merge Two Sorted Lists (Java)
The key to solve the problem is defining a fake head. Then compare the first elements from each list. Add the smaller one to the merged list. Finally, when one of them is empty, simply append it to the merged list, since it is already sorted.
1 | /** |
Submission Detail
- 208 / 208 test cases passed.
- Runtime: 1 ms, faster than 20.84% of Java online submissions for Merge Two Sorted Lists.
- Memory Usage: 40.2 MB, less than 12.09% of Java online submissions for Merge Two Sorted Lists.
Recursive
1 | /** |
Submission Detail
- 208 / 208 test cases passed.
- Runtime: 13 ms
- Your runtime beats 34.74 % of java submissions.