DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

Day 5 of My Competitive Programming Journey: Unlocking the Power of Linked Lists

Hello Everyone!

I am Somuya Khandelwal, and I’m here to share the exciting progress I made on Day 5 of my competitive programming journey. After working through queues yesterday, today I ventured into the world of Linked Lists—one of the most fundamental yet versatile data structures in programming.

Linked lists may seem simple at first glance, but their flexibility and dynamic nature make them indispensable for solving a wide range of problems. My focus today was to strengthen my understanding of linked lists by solving three medium and two hard problems on Leetcode.


What I Worked On Today

  1. Reverse a Linked List (Medium Difficulty)

    • This classic problem involved reversing a singly linked list.
      • I learned to manipulate pointers (prev, curr, and next) to reverse the links between nodes iteratively.
      • For an added challenge, I also implemented the recursive approach, which deepened my understanding of stack-based recursion in linked lists.
  2. Merge Two Sorted Linked Lists (Medium Difficulty)

    • The task was to merge two sorted linked lists into a single sorted list.
      • Using a dummy node for simplicity, I compared the nodes from both lists one at a time, ensuring an efficient merge.
      • This problem taught me how to manage edge cases, such as when one list is empty or significantly shorter than the other.
  3. Find the Middle of a Linked List (Medium Difficulty)

    • A straightforward yet insightful problem, this involved finding the middle node of a linked list.
      • I used the two-pointer technique, where one pointer moves twice as fast as the other. This approach efficiently finds the middle node in a single traversal.
  4. Detect a Cycle in a Linked List (Hard Difficulty)

    • The goal was to determine if a linked list contained a cycle.
      • Using Floyd’s Tortoise and Hare algorithm, I efficiently detected cycles by checking if two pointers (one moving faster than the other) ever met.
      • Implementing this algorithm taught me the importance of pointer manipulation in solving linked list problems.
  5. Flatten a Multilevel Doubly Linked List (Hard Difficulty)

    • This complex problem required flattening a doubly linked list with multiple levels into a single-level list.
      • Using recursion, I traversed each level, connecting nodes while ensuring the structure remained intact.
      • Debugging this problem helped me appreciate the intricacies of pointer-based data structures.

What I Learned Today

  1. Pointer Manipulation:

    • Working with linked lists requires precise pointer handling. A single mistake can lead to null pointer exceptions or infinite loops, so attention to detail is critical.
  2. Two-Pointer Technique:

    • This elegant approach, used in problems like finding the middle node and detecting cycles, is a must-have tool for any programmer’s arsenal.
  3. Recursive vs Iterative Solutions:

    • Comparing recursive and iterative solutions for reversing linked lists highlighted the trade-offs between simplicity (recursion) and efficiency (iteration).
  4. Practical Applications of Linked Lists:

    • From dynamic memory allocation to implementing stacks and queues, linked lists have endless real-world applications. Today’s problems showed me how versatile this data structure can be.

Reflections and Challenges

Day 5 was both rewarding and challenging. The Flatten a Multilevel Doubly Linked List problem tested my patience, as debugging recursive pointer manipulations was no small feat. However, the satisfaction of finally getting it right made the effort worthwhile.

Linked lists might seem like an academic topic, but solving these problems demonstrated their relevance in solving real-world challenges. The hands-on experience has also boosted my confidence in handling pointer-based data structures.


Looking Ahead

With the first week of my competitive programming journey coming to an end, I’m feeling motivated to push forward. Next week, I plan to explore Dynamic Programming, a powerful paradigm that unlocks solutions to some of the most complex algorithmic problems.

Thank you for being part of my journey so far! I hope my experiences inspire you to take on your own challenges and explore the limitless potential of problem-solving. Stay tuned for more updates as I continue to learn and grow.


Top comments (0)