﻿1
00:00:00,400 --> 00:00:05,920
‫So in this video, we're going to do the introduction to delete last.

2
00:00:06,130 --> 00:00:10,750
‫And in the next video, we'll write the code for this function.

3
00:00:11,170 --> 00:00:18,580
‫And the reason I split this into two videos is because removing and deleting the last item in a linked

4
00:00:18,580 --> 00:00:25,030
‫list is far more complicated than adding an item to the end of the linked list.

5
00:00:25,480 --> 00:00:29,620
‫So to explain, I'm going to bring up a link to list like this.

6
00:00:29,950 --> 00:00:32,990
‫And we have three situations to code for.

7
00:00:33,010 --> 00:00:39,040
‫First, if we have two or more items, we're going to move tail over to the previous node and delete

8
00:00:39,040 --> 00:00:41,710
‫the node at the end of the length list.

9
00:00:42,040 --> 00:00:48,640
‫We also have to code for when we have an empty linked list, but we also have another edge case for

10
00:00:48,640 --> 00:00:52,240
‫when we have one item in the length list.

11
00:00:52,570 --> 00:00:58,810
‫So if you want to challenge, pause the video here and see if you can code this function.

12
00:00:59,230 --> 00:01:06,160
‫Otherwise, I'm going to get into the steps for removing a node when we have two or more items in the

13
00:01:06,160 --> 00:01:07,210
‫linked list.

14
00:01:07,480 --> 00:01:14,470
‫So it looks easy to move tail over to the previous node and then just delete the last node.

15
00:01:14,710 --> 00:01:21,880
‫But to explain why this is more complicated than it looks, I'm going to put this node back, move tail

16
00:01:21,880 --> 00:01:28,570
‫over and turn this length list into a set of unordered maps.

17
00:01:28,840 --> 00:01:33,130
‫So this is very similar to the way Linked list would work.

18
00:01:33,310 --> 00:01:40,450
‫So what we're wanting to do is move tail up to this node and then we'll set tail next to be equal to

19
00:01:40,450 --> 00:01:44,320
‫null pointer and delete that last node.

20
00:01:44,740 --> 00:01:49,900
‫But let's put everything back and look at what we need to do to make that happen.

21
00:01:50,140 --> 00:01:58,450
‫In order to set tail to be equal to this node, we need to set tail equal to a pointer that points at

22
00:01:58,450 --> 00:01:59,140
‫this node.

23
00:01:59,140 --> 00:02:04,660
‫And the only pointer that points at it is next in this node.

24
00:02:04,960 --> 00:02:07,270
‫So that means we need to get to this node.

25
00:02:07,270 --> 00:02:16,510
‫And the only way to get to this node is through next in this node and so on until we get to the head.

26
00:02:16,930 --> 00:02:23,650
‫So we have to have a pointer that starts at the head and then iterates through the length list until

27
00:02:23,650 --> 00:02:25,450
‫it gets to this node.

28
00:02:25,570 --> 00:02:29,560
‫And then we can set tail to be equal to this pointer.

29
00:02:29,890 --> 00:02:36,610
‫Then we can set tail next to be equal to null pointer and delete the last node.

30
00:02:37,030 --> 00:02:39,910
‫So let's put this back to its graphical form.

31
00:02:40,120 --> 00:02:45,670
‫And in order to do this, we're going to create a couple of variables called pre and temp will set them

32
00:02:45,670 --> 00:02:46,930
‫equal to head.

33
00:02:47,170 --> 00:02:53,470
‫And for now I'm going to remove head and tail and I'm going to move pre underneath and we're going to

34
00:02:53,470 --> 00:02:56,470
‫create a loop that goes until temp.

35
00:02:56,470 --> 00:03:02,860
‫Next is equal to null pointer, which will end up happening when we get to the end of the length list.

36
00:03:03,250 --> 00:03:06,730
‫So we start out by saying temp next.

37
00:03:06,760 --> 00:03:12,430
‫If that's pointing to a node, we're going to set pre equal to temp, and in the first iteration it's

38
00:03:12,430 --> 00:03:17,740
‫already equal to temp, and then we'll set temp to be equal to temp next.

39
00:03:17,740 --> 00:03:19,420
‫And that moves that over.

40
00:03:19,720 --> 00:03:21,700
‫And then we're going to do it again.

41
00:03:21,700 --> 00:03:28,990
‫We move pre up temp over and then we just keep iterating through the linked list and this will be the

42
00:03:28,990 --> 00:03:30,760
‫last time we do this.

43
00:03:31,000 --> 00:03:34,120
‫We move pre up temp over.

44
00:03:34,360 --> 00:03:40,990
‫Now temp next is equal to null pointer that breaks us out of our loop and pre and temp.

45
00:03:40,990 --> 00:03:42,700
‫Stay right here.

46
00:03:43,060 --> 00:03:51,730
‫So for now I'm going to remove temp and we'll set tail to be equal to pre and that's the only thing

47
00:03:51,730 --> 00:03:53,140
‫we're using pre for.

48
00:03:53,170 --> 00:03:55,180
‫So I'm going to drop that out.

49
00:03:55,630 --> 00:04:03,430
‫We'll set tail next to be equal to null pointer that breaks that node off of the end of the length list.

50
00:04:03,640 --> 00:04:05,110
‫And I want to bring back temp.

51
00:04:05,110 --> 00:04:07,240
‫Remember it was pointing to this node.

52
00:04:07,240 --> 00:04:11,950
‫We will say delete temp and that removes that node.

53
00:04:12,190 --> 00:04:14,770
‫So we will code this in the next video.

54
00:04:14,770 --> 00:04:19,840
‫But for now that is our introduction to delete last.

