DEV Community

Saranya R
Saranya R

Posted on

Merge Two Linked List

Approach Explanation (ノ◕ヮ◕)ノ*:・゚✧

  1. I created a dummy node to simplify handling the head of the merged list.
  2. I used a tail pointer to build the merged list step by step.
  3. I compared the current nodes of both lists:
  4. If list1’s value was smaller, I attached it.
  5. Otherwise, I attached list2.
  6. I moved forward in whichever list I used.
  7. When one list was empty, I attached the remaining nodes of the other list.
  8. Finally, I returned the merged list starting from dummy.next.

Method Used: (〜 ̄▽ ̄)〜

  • Dummy node technique
  • Two-pointer comparison
  • In-place splicing
  • Efficient traversal
  • Constant space

Top comments (0)