Problems Solved:
- #169 Majority Element
- #206 Reverse Linked List
This continues my daily series (Day 14: Voting Algorithm + Pointer Manipulation). Today I explored the BoyerβMoore majority vote algorithm and linked list reversal via iterative pointer updates.
π‘ What I Learned
- For Majority Element, the BoyerβMoore Voting Algorithm provides an elegant O(n) time and O(1) space solution. By tracking a candidate and a counter, we can always converge to the majority element (guaranteed to exist).
- For Reverse Linked List, pointer manipulation is the key. Keeping track of
prev
,current
, andnext
ensures that links are reversed step by step until the list is fully flipped. - Both problems highlight how state tracking with minimal variables can solve classic linked list and array challenges efficiently.
π§© #169 β Majority Element
Python (BoyerβMoore Voting)
class Solution:
def majorityElement(self, nums: List[int]) -> int:
candidate = None
count = 0
for x in nums:
if count == 0:
candidate = x
count += 1 if x == candidate else -1
return candidate
C++ (BoyerβMoore Voting)
class Solution {
public:
int majorityElement(vector<int>& nums) {
int candidate = 0;
int count = 0;
for (int x : nums){
if(count == 0){
candidate = x;
}
if(x == candidate){
count += 1;
} else {
count -= 1;
}
}
return candidate;
}
};
Why it works: majority element is guaranteed to exist, so the voting process never fully cancels it out.
Time: O(n)
Space: O(1)
π§© #206 β Reverse Linked List
Python (Iterative Reversal)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
current = head
while current is not None:
nxt = current.next
current.next = prev
prev = current
current = nxt
return prev
C++ (Iterative Reversal)
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* current = head;
while (current != nullptr){
ListNode* nxt = current->next;
current->next = prev;
prev = current;
current = nxt;
}
return prev;
}
};
Why it works: each nodeβs next
pointer is reversed one by one, while prev
becomes the new head at the end.
Time: O(n)
Space: O(1)
πΈ Achievements
- Majority Element (Python & C++)
- Reverse Linked List (Python & C++)
π¦ Complexity Recap
- Majority Element: linear in array size, constant space.
- Reverse Linked List: linear in list size, constant space.
π£ Join the Journey
Iβm solving and documenting problems daily in both Python and C++, covering hashing, pointers, recursion, and dynamic programming. Follow along if youβre interested in systematic problem solving.
Links
- LinkedIn: https://www.linkedin.com/in/ertugrul-mutlu
- GitHub Series: https://github.com/Ertugrulmutlu/leetcode-series
Top comments (0)