DEV Community

Ertugrul
Ertugrul

Posted on

πŸ—“ Daily LeetCode Progress – Day 14

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, and next 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
Enter fullscreen mode Exit fullscreen mode

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;
    }
};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
    }
};
Enter fullscreen mode Exit fullscreen mode

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++)

maj py

maj cpp

  • Reverse Linked List (Python & C++)

rev py

rev cpp


πŸ“¦ 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

Top comments (0)