LeetCode 21 – Merge Two Sorted Lists [C Solution] | Linked Lists in C
Welcome to CodeWithBK — your complete learning hub for mastering C programming concepts.
🔎 What You'll Learn in this video:
Problem: Combine two sorted linked lists (l1 and l2) into a single sorted list, reusing their existing nodes.
Algorithm Outline:
Check if either list is empty—if so, return the other list immediately.
Determine which initial node will be the head of the merged list.
Use two pointers (curr1, curr2) to iterate through l1 and l2, and a pointer tail that always tracks the last node of the merged list.
Compare values, link the smaller node to tail's next, advance the corresponding source pointer and tail.
When one list runs out, append the rest of the other list.
Return the head pointer. (No dummy node allocations required.)
This method achieves O(m + n) time and O(1) additional space, fully in‑place using only existing nodes
🧠 Concepts Covered: Linked list basics, pointer manipulation, iterative solution
✅ Why This Approach?
Cleaner Memory Usage: No dummy node means no extra (even temporary) nodes.
Simplified Control Flow: Initialization establishes a proper head, removing post‑loop pointer juggling.
Efficient & In-Place: Only existing nodes are touched, maintaining O(1) aux space and linear time.
🧠 Perfect for:
C programming learners
Coding interview preparation
Practicing linked list manipulation
📚 Related Videos:
LeetCode 92 C Solution – Reverse Linked List II : • LeetCode 92 – Reverse Linked List II [C So...
LeetCode 707 C Solution – Design a Linked List : • LeetCode 707 – Design Linked List [C Solut...
LeetCode 19 C Solution – Remove Nth Node From End of List : • LeetCode 21 – Merge Two Sorted Lists [C So...
👨💻 Timestamps:
00:00 – Introduction
00:44 – Problem description
01:47 – algorithm and solution
08:02 – C Code implementation
14:54 – testing the code
👍 Don't forget to like, comment, and subscribe for more tutorials on C programming, LeetCode solutions and other coding topics!
Stay connected:
Subscribe: / @codewithbk
Email: [email protected]
Discord: / discord
Instagram ▶️ / balkrishna_srivastava
Coding mentor profile: https://www.codementor.io/@balkrishna...
For 1:1 sessions, reach out to me here : https://www.codementor.io/@balkrishna...
My YouTube channel where I teach in Hindi : / @codewithbkhindi
#CProgramming #CodeWithBK #LinkedList #DataStructures #LeetCode #CForBeginners #PointersInC #LearnCProgramming #CPrograms #LeetCodeSolution