00:01
We're going to discuss the runtimes of inserting a node in a link list as well as deleting a node in a link list.
00:07
We're going to start with inserting.
00:09
Here i have some pseudocode on the left that's written up to describe the position and the data that's being going to be inserted.
00:19
Those are the parameters.
00:20
There's an if statement catching the position, a parameter, if it's within bounds.
00:26
And if it isn't it's going to print out to the user that their position is invalid.
00:31
If that check passes, what's going to happen is the program's going to enter a while loop and once pos decriments to zero, what's going to happen is a new node is going to be created and the temp node, the current node is going to point towards that temp node, and then the temp node is also going to point towards that temp node, and then the temp node is also going to point towards the next node that way it kind of inserts itself in a way that allows for the list to continue and the rest of the function or the rest of the program looks to just iterate through the list until it decrements to zero where it knows to insert that value and size obviously increases if it's the size of the list increases by one once the while loop is done so to demonstrate what's happening in writing here we're going to just create a mock link list here.
01:28
We have two scenarios that can play out with the run times.
01:32
One is a worst case, one is a middle case, and one is a best case.
01:40
So say we, we'll start with the best case.
01:43
If we have a head here on our link list and say we want to insert a new head essentially, what's going to happen is the pos 0 is going to kick right away.
01:57
And so that's going to give a constant runtime of or big o 1.
02:01
So this is no longer the head.
02:03
This is the head.
02:05
And it's next.
02:06
It's just going to point where the old head was with that temp note that was created.
02:12
So this one is a big o notation of 1 or current, which is pretty good.
02:18
Second is going to be, let's discuss what happens when it's in the middle of the list...