5. Merge Two Sorted Lists (Easy)
π Problem Link
π§± Context:
- Written in C++.
- Merges two sorted singly linked lists into a new sorted list.
- A singly linked list is a structure where each node holds a value (
val
) and a pointer to the next node (next
).
π§© ListNode Structure
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
-
val
: value of the node -
next
: pointer to the next node
Three constructors:
- Default:
val = 0
,next = nullptr
- With value only
- With value and next node pointer
π§ Solution Class
class Solution {
public:
- Class for our solution
-
public:
means the method is accessible externally
π§ mergeTwoLists
Method
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
- Takes two pointers to sorted lists
- Returns pointer to a new merged sorted list
π§± Step 1: Create dummy starting node
ListNode* dummyHead = new ListNode();
Used as a fake starting point to simplify appending new nodes.
π Step 2: Create traversal pointer
ListNode* current = dummyHead;
current
moves along the new list.
π Step 3: While both lists aren't finished
while (list1 && list2) {
Continue while both lists have nodes.
π Step 4: Compare values
if (list1->val <= list2->val) {
Choose the smaller value.
β‘οΈ Step 5: Append from list1
current->next = list1;
list1 = list1->next;
π Otherwise, take from list2
} else {
current->next = list2;
list2 = list2->next;
}
π§ Step 6: Move forward in the new list
current = current->next;
π Step 7: Attach remaining nodes
current->next = list1 ? list1 : list2;
π Step 8: Return the merged list
return dummyHead->next;
Return the first real node (skip dummy).
β Summary:
- Create a dummy node
- Traverse both lists
- Choose the smaller node each time
- Attach the rest when one list ends
- Return merged result
6. Reverse Linked List (Easy)
π Problem Link
πΉ ListNode Structure
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
π reverseList
Function
ListNode* reverseList(ListNode* head) {
- Input: pointer to start of the list
- Output: pointer to start of reversed list
Steps:
- Create dummy head
ListNode* dummy = new ListNode();
- Pointer for traversal
ListNode* current = head;
- Loop through list
while (current != nullptr) {
- Save the next node
ListNode* nextNode = current->next;
- Reverse pointer
current->next = dummy->next;
- Insert at start of new list
dummy->next = current;
- Move to next
current = nextNode;
- Return reversed list
return dummy->next;
π§© Visual Walkthrough
Original:
head β 1 β 2 β 3 β 4 β nullptr
After iterations:
dummy β 1
dummy β 2 β 1
dummy β 3 β 2 β 1
dummy β 4 β 3 β 2 β 1
π‘ Recap:
- Create empty list
- Take nodes one by one from original
- Insert at beginning of new list
- Return result
7. Linked List Cycle (Easy)
π Problem Link
π¦ Node Structure
struct ListNode {
int value;
ListNode *next;
ListNode(int x) : value(x), next(nullptr) {}
};
π§ Cycle Detection (hasCycle
)
bool hasCycle(ListNode *head) {
π’π Use two pointers:
ListNode *slowPointer = head;
ListNode *fastPointer = head;
-
slowPointer
moves 1 step -
fastPointer
moves 2 steps
π Traverse the list
while (fastPointer && fastPointer->next) {
slowPointer = slowPointer->next;
fastPointer = fastPointer->next->next;
If they meet:
if (slowPointer == fastPointer)
return true;
No cycle?
return false;
π Visual
Without cycle:
1 β 2 β 3 β 4 β nullptr
With cycle:
1 β 2 β 3 β 4
β
β
2 (loop)
π§© Summary:
- Two pointers moving at different speeds
- If cycle exists β they'll meet
- If not β fast pointer hits end
8. Maximum Subarray (Medium)
π Problem Link
π¦ The Problem:
Given an array, find the maximum sum of any continuous subarray.
Example:
Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6
β from [4, -1, 2, 1]
π§ Kadaneβs Algorithm
int maxSubArray(vector<int>& nums) {
πΉ Step 1: Initialize max values
int currentMax = nums[0];
int globalMax = nums[0];
π Step 2: Iterate
for (int i = 1; i < nums.size(); ++i) {
currentMax = std::max(currentMax, 0) + nums[i];
globalMax = std::max(globalMax, currentMax);
}
β Step 3: Return result
return globalMax;
π§© Example Breakdown
i | nums[i] | currentMax | globalMax |
---|---|---|---|
0 | -2 | -2 | -2 |
1 | 1 | 1 | 1 |
2 | -3 | -2 | 1 |
3 | 4 | 4 | 4 |
4 | -1 | 3 | 4 |
5 | 2 | 5 | 5 |
6 | 1 | 6 β | 6 |
7 | -5 | 1 | 6 |
8 | 4 | 5 | 6 |
π‘ Summary:
-
currentMax
: sum of current subarray - Reset to 0 if it turns negative
-
globalMax
: max seen so far - One pass: O(n) complexity
β Follow-up
If you liked this breakdown, let me know β I plan to continue this series with more Leetcode problems, especially in C++ with deep explanations for beginners.
Top comments (0)