DEV Community

vishal.codes
vishal.codes

Posted on

Day 26/366

🚀 Today's Learning:

🌟 DSA

  • Check for a cycle in a linked list
  • Find the junction point in a linked list having a cycle

🌟 Dev

  • custom Hooks in React

🔍 Some Key Highlights:

DSA

To check for a cycle in a linked list, you use two pointers: slow and fast. Both start at the head of the list. In each iteration, the slow pointer moves one step forward while the fast pointer moves two steps forward. If there's a cycle, eventually the fast pointer will catch up to the slow pointer. If at any point the fast pointer reaches the end of the list, there's no cycle. This algorithm is efficient and uses constant space, making it suitable for large linked lists.

To find the junction point of a cycle in a linked list, you first determine if there's a cycle using the aforementioned approach. Once you find the meeting point of the slow and fast pointers, you reset one pointer to the head of the list and keep the other at the meeting point. Then, you move both pointers forward one step at a time until they meet again; this time, at the junction point of the cycle. This method efficiently identifies the junction point of a cycle in a linked list without requiring extra space.

DEV

Custom hooks in React are reusable functions that allow you to extract component logic into separate functions. You create a custom hook by prefixing its name with "use" to follow the convention. These hooks can encapsulate any kind of logic, such as fetching data, managing state, or handling subscriptions. Custom hooks enable you to share logic between components without having to resort to higher-order components, render props, or other patterns. By creating custom hooks, you promote code reusability and maintainability in your React applications.

#100daysofcode #1percentplusplus #coding #dsa

Top comments (0)