📖 Scenario: You are working on a contact app that keeps two separate lists of contacts sorted by their ID numbers. You want to combine these two sorted lists into one sorted list so that you can show all contacts in order.
🎯 Goal: Build a program that merges two sorted linked lists of integers into one sorted linked list.
📋 What You'll Learn
Create two sorted linked lists using the provided ListNode struct
Create a helper variable to start the merged list
Write the core logic to merge the two sorted linked lists
Print the merged linked list in the format: 1 -> 2 -> 3 -> null
💡 Why This Matters
🌍 Real World
Merging sorted lists is common in apps that combine data from multiple sources, like contact lists or event schedules.
💼 Career
Understanding linked list merging is important for software engineers working with data structures, algorithms, and optimizing data processing.
Progress0 / 4 steps
1
Create Two Sorted Linked Lists
Create two sorted linked lists using the ListNode struct. The first list should have nodes with values 1, 3, and 5. The second list should have nodes with values 2, 4, and 6. Use variables list1 and list2 for the heads of these lists.
DSA Go
Hint
Use the &ListNode{Val: value, Next: nextNode} syntax to create linked list nodes.
2
Create a Dummy Node to Start the Merged List
Create a variable called dummy as a new ListNode with value 0. Also create a variable called current that points to dummy. These will help build the merged list.
DSA Go
Hint
Use dummy := &ListNode{Val: 0} and current := dummy.
3
Merge the Two Sorted Lists
Write a for loop that continues while both list1 and list2 are not nil. Inside the loop, compare list1.Val and list2.Val. Attach the smaller node to current.Next, then move current and the chosen list forward by one node. After the loop, attach the remaining nodes from list1 or list2 to current.Next.
DSA Go
Hint
Use a for loop with condition list1 != nil && list2 != nil. Inside, compare list1.Val and list2.Val to attach the smaller node.
4
Print the Merged Linked List
Write a for loop to print the merged linked list starting from dummy.Next. Print each node's value followed by " -> ". After the loop, print "null" to show the end of the list.
DSA Go
Hint
Use a for loop starting at dummy.Next to print each node's value followed by " -> ". After the loop, print "null".
Practice
(1/5)
1. What is the main goal when merging two sorted linked lists?
easy
A. Remove duplicates from both lists without merging
B. Sort each list separately without combining them
C. Create a new sorted linked list containing all elements from both lists
D. Reverse both lists and then merge
Solution
Step 1: Understand the merge operation
Merging means combining two lists into one while keeping the order sorted.
Step 2: Identify the goal of merging sorted lists
The goal is to create a new sorted list that contains all elements from both input lists.
Final Answer:
Create a new sorted linked list containing all elements from both lists -> Option C
Quick Check:
Merge = Combine sorted lists [OK]
Hint: Merging means combining sorted lists into one sorted list [OK]
Common Mistakes:
Thinking merge means sorting each list separately
Confusing merge with removing duplicates only
Assuming merge reverses the lists
2. Which of the following is the correct Go syntax to define a node struct for a singly linked list?
easy
A. Node := struct { val int; next Node }
B. struct Node { int val; Node* next; }
C. class Node { int val; Node next; }
D. type Node struct { val int; next *Node }
Solution
Step 1: Recall Go struct syntax
In Go, structs are defined with 'type Name struct { fields }' and pointers use '*'.
Step 2: Check each option
type Node struct { val int; next *Node } uses correct Go syntax with pointer '*Node' for next node.
Final Answer:
type Node struct { val int; next *Node } -> Option D
Quick Check:
Go struct with pointer = type Node struct { val int; next *Node } [OK]
Hint: Go structs use 'type' and pointers with '*' [OK]
Common Mistakes:
Using C/C++ or Java syntax in Go
Missing pointer '*' for next node
Using := inside struct definition
3. What is the output linked list after merging these two sorted lists?