 What's up guys my name is Michael and welcome to my YouTube channel today. We're gonna do another leak code easy This is called middle of the link list So I actually skipped three problems that I didn't actually which I did finish But I didn't actually create a video for it But here are some the three problems that I didn't create a video of even though it's part of the be code easy So yeah, this is one of them middle of the link list given a non-empty string You just want to return the middle of the link list Given a lot non-empty single link list you just want to return the middle of them so originally my solution was to basically keep going to the Go through the whole link list and add up the length of the number of nodes inside the link list Once you do that you divide by two and then I go through the link list again until I reach that number That I divided it by right that then the length over two and that would be the middle But there's actually a much easier way to do this which is actually to have two two two pointers pointing from the Essentially you have one pointer pointing at the beginning and The say is another point of pointing at the beginning also, but what you're gonna do is you're gonna have one pointer Go twice as fast as the first one and then when that pointer reaches to the end Your first pointer is going to be at the middle Do you guys don't understand what I'm saying? So if you have like if you have Let's say you have this node point to here Let's say you have like One two three values one two three So what essentially what we're gonna do is we're gonna have two One pointing here one point here is gonna be the fast one I'll call it F fast okay fast and this is gonna be the slow one and basically fast is gonna Go twice as fast as a slow one. So it's gonna go Dot next dot next so fast is gonna go here and then slow just gonna go Go to the next value and when So it's gonna go to the next value. So it's just gonna go here. So then when fast reaches to the end Slow is gonna be in the middle. So that's basically how it works So I'm gonna code that right now and then see I'm gonna give you guys how to do it. So we're gonna have like this node Begin is going to point to the beginning Actually not beginning. We're gonna make it fast and we're gonna have a Pointer pointing to the slow also to the beginning. Okay, so while fast fasts next value While slow is not equal to null and Fasts next value is not equal to null. What we're going to do is fast is gonna go to fast next fasts Equal to okay slow is gonna equal to slows next Fasts next value is going to equal to Fast next next Not fast next fast. It was a fast next next Okay, and basically in the end you're gonna just return slow. That's basically how this would work Um, let's see. Yeah one three five. Okay. That's expected submit it. Okay. We got runtime error. There's a null pointer Oh, let's see No pointer with a null point of title. Let's not do to do to fasts. No so fasts Slow is not equal to null fast next fast next Think it's like this No, that's not it. That's not it. Okay. I think it's we have to check if the next Is not equal to null. Just a minute Runtime error. Let me see. All right. Well, let me let me just look at my solution again one more time Yeah fast next fast again the next next fast next fast. Oh, okay So yeah, we have to check if fasts are not equal to null my bad. So fast Is not equal to null and yeah It's basically it. It's basically the solution Yeah, and I got accepted So just keep a two pointers and make you keeping track one is gonna go twice as fast as the first one the first one It's gonna go just the next one and then when the the fast one reaches the end the slow is gonna be in the middle So yeah, that's how you do this question rate com subscribe. Hope you guys enjoy this video. Peace