Find the Middle of a Singly LinkedList[Technique Tuesdays]

Understanding a neat little Leetcode Trick

How can you find the middle of a singly linked list?

What is the reasoning behind this approach?

And how can you use it in your software engineering?

These are all questions I will be answering in today’s email/post.

Photo by Florian Olivo on Unsplash

But before we proceed, I have an amazing announcement for y’all. We have a lot of amazing Tech Professionals in our community. So I’ll be partnering with recruiters/headhunters to help people find roles. If you’re a premium subscriber to this newsletter, you will be sent open roles by a variety of people. You can read through them, and give some basic information about yourself. I will then forward your profile to the recruiters, at no additional cost to you.

Most of the roles will be based in/around the USA, but if you request, I will work on integrating other regions.

If you’re looking for new roles, this will be a great help to you. I will share the information with you soon.

Onwards with the post

The approach discussed is the basis of a famous cycle detection algorithm. Image Source

Find the middle of a singly linked list

  1. How to find the middle- The standard advice is to use 2 pointers, one fast and the other slow (as shown in the animation above). This is pretty simple. And it works very well, so no reason to reinvent the wheel. Most of you are the level where I would need to hand-hold you through the explanation. If you would like to brush up on it, I will link a video at the end. Here is the code for it-
def findMiddle(self):
fast_ptr = self.head
slow_ptr = self.head

if(self.head is not None):
while(fast_ptr is not None and fast_ptr.next is not None):
fast_ptr = fast_ptr.next.next
slow_ptr = slow_ptr.next
print('The middle element is ' + str (slow_ptr.data))
  1. The reasoning behind this- A lot of people will tell you how this works, and why it’s correct mathematically. But that won’t help you in the interview/your work when you have to come up with this on the fly. In Math, we have the concept of an ordered structure, where a set of values can be compared for equality/inequality. While the values of a LinkedList are not ordered, their indices are. Furthermore, they form a line, allowing you to split them into multiple equal portions. This algorithm is just a special case, where we bisect it. This technique takes advantage of that property, allowing you to break
  2. Why this matters- LinkedLists are relatively simple structures, with Linear ordering. So this property isn’t going to shock any of you. However, you can extend this analysis to more complex groups, including objects, datasets, and organizational structures (I’ve worked on improving operational efficiency using AI using this technique). Learning to think in abstract groups and structures instead of specific data types will make you much better at creative problem-solving since you will be able to take a top-level view of situations.
Multiple supervisors have praised my novelty in problem-solving. This has been a big reason. Also, I will be entering the job-market soon. If you want to work with me/have any leads, please reach out using LinkedIn

To those of you looking for a refresher on this idea, here is a video going over the basics of this concept.

That is it for this piece. I appreciate your time. As always, if you’re interested in reaching out to me or checking out my other work, links will be at the end of this email/post. If you like my writing, I would really appreciate an anonymous testimonial. You can drop it here. And if you found value in this write-up, I would appreciate you sharing it with more people. It is word-of-mouth referrals like yours that help me grow.

Upgrade your tech career with a premium subscription ‘Tech Made Simple’! Stay ahead of the curve in AI, software engineering, and tech industry with expert insights, tips, and resources. 20% off for new subscribers by clicking this link. Subscribe now and simplify your tech journey!

Using this discount will drop the prices-

800 INR (10 USD) → 533 INR (8 USD) per Month

8000 INR (100 USD) → 6400INR (80 USD) per year

Get 20% off for 1 year

Reach out to me

Use the links below to check out my other content, learn more about tutoring, reach out to me about projects, or just to say hi.

If you like my writing, I would really appreciate an anonymous testimonial. You can drop it here.

To help me understand you fill out this survey (anonymous)

Small Snippets about Tech, AI and Machine Learning over here

Check out my other articles on Medium. : https://rb.gy/zn1aiu

My YouTube: https://rb.gy/88iwdd

Reach out to me on LinkedIn. Let’s connect: https://rb.gy/m5ok2y

My Instagram: https://rb.gy/gmvuy9

My Twitter: https://twitter.com/Machine01776819

--

--

Writeups from my Substack newsletter, Technology Made Simple. You can find it on my Substack- https://codinginterviewsmadesimple.substack.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
D

Writeups from my Substack newsletter, Technology Made Simple. You can find it on my Substack- https://codinginterviewsmadesimple.substack.com/