Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Exercise 7 Solution Part 2" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- Brian finishes the solution to exercise 7.

Get Unlimited Access Now

Transcript from the "Exercise 7 Solution Part 2" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Brain: So let's do delete first, cuz we're gonna define pop in terms of delete.
>> Brain: So if (index === 0) { const head = this.head; if (head) { this.head = head.next; }
>> Brain: Else { this.head = null; } this.length--; return head.value;. So basically, the first thing we're doing is we're checking to see if we're trying to delete the head.

[00:00:54] And if we're trying to delete the head then, we kinda have to do some special handling around that because we have to be able to point at the correct head. We're also not really handling the tail right here, and if head's gonna be null then tail's also gonna be null.

[00:01:09] So, you can also say this.tail = null; as well.
>> Brain: Yeah, that'll work. I'm not testing for it which is why it's not doesn't this that have to be in there, okay? So that handles that special case otherwise, make sure I'm in the right one, okay? So you're gonna say, const node = this._find(index-1), right?

[00:01:41] So we're looking for the thing that's before it, and then we're gonna use this.testIndex to find that, right?
>> Brain: Then the node that we're gonna excise, it's gonna be const excise = node.next, right? That's what we're actually gonna end up deleting and then if there is no excise

[00:02:11]
>> Brain: Then you're just gonna return null. If there's nothing to delete then don't delete it, that's fine. Okay, node.next = excise.next. So basically grab the node that you're gonna delete, and then take its next and make that the previous node's next.
>> Brain: And here, this one's fun. If node.next.next, so you're going two hops now.

[00:02:44] Then this is the tail right? That's basically what's going on here. So this.tail = node.next; you can wrap that into curly braces if that helps.
>> Brain: And then finally, you're gonna document the length no matter what and return next A stop value.
>> Speaker 2: You're testing next above these in underscore.

[00:03:14]
>> Brain: Thank you. Yes it does. Return excise.value.
>> Brain: Okay, so now we made delete work, okay good, you can see down here that's working. So now let's go implement pop in terms of delete, so we're not repeating ourselves. So if you look at the answer actually do separate it, and do pop and delete separately.

[00:03:44] But you don't have to, so pop() { return this.delete (this.length-1)
>> Brain: Can't read properly next of null, can't read property next to null. Let's see, get rid of that.
>> Brain: So that's gonna be wrong, cuz it's gonna delete the wrong thing.
>> Brain: Did I implement this? I must have not implemented this correctly.

[00:04:46] So, let's step through it.
>> Brain: Again, the dangers of live coding, right?
>> Speaker 2: [LAUGH]
>> Brain: [LAUGH] Okay, so passing in here index. So let's just say, console.log(index);
>> Brain: And get the console up here as well.
>> Brain: Okay, so now we're getting.
>> Brain: So if it's the tail, it' not gonna hit the indexes equals zero, right?

[00:05:37] So here, we must do find. And find using test index for the last index in there, so that's fine. That should work. Should find the ones just before it. And then this should be the tail, right? Node.next.
>> Brain: I think you have to say, if node.next.
>> Brain: That might be it.

[00:06:12] Yep. So I was referencing next right here, which in some cases, well in the case of popping off the back, the node.next was gonna be null, so you can't look at next of null, right? That's what was happening. Okay, that was a bug, congratulations, we found it. And there we go, linked list.

[00:06:35] Ta-da! [LAUGH] It's like a magic trick. Just a long drawn out magic trick. Okay, so any questions about Linked Lists? Good news as we are done with lists [LAUGH]. I feel like when you're talking about data structures and algorithms you just have to talk about those right? Because the trade offs that you make with, they're not super fun to implement but the trade off's that you have to make to enable to decide if I want an array list or a leans list.

[00:07:10] I feel like those are important trade offs to be able to be making in your mind, right? Do I need to be optimizing for reads and deletes. Basically the mind path of going down, what am I going to be doing and what can I optimize for, right? That's important ways to be thinking about problems.

[00:07:26] And those are trade offs that we as web developers have to make a lot. So you might not necessarily be, am I gonna use links list or anything like that but it might be I don't know, other code paths you might have to optimize for. It's the way of thinking that I really want to impart to you.