﻿1
00:00:00,430 --> 00:00:04,600
‫So now we're going to write the code for delete last.

2
00:00:04,630 --> 00:00:07,030
‫We'll start the code out like this.

3
00:00:07,390 --> 00:00:12,520
‫And the first thing we'll code for is for when we have an empty linked list.

4
00:00:12,850 --> 00:00:16,650
‫We'll do that like this if the length is equal to zero.

5
00:00:16,660 --> 00:00:22,780
‫Or we could say if head is equal to null pointer, just some kind of test to see if the length list

6
00:00:22,780 --> 00:00:23,740
‫is empty.

7
00:00:23,770 --> 00:00:29,020
‫If it is, we're just going to run return, which breaks us out of this function.

8
00:00:29,230 --> 00:00:36,010
‫And now the next thing we'll code for is when we have two or more items in the linked list and we're

9
00:00:36,010 --> 00:00:37,570
‫going to have those two variables.

10
00:00:37,570 --> 00:00:43,990
‫Temp and pre will create them like this temp equals head and pre equals head.

11
00:00:44,110 --> 00:00:48,670
‫And now that we have those, we can start creating our wild loop.

12
00:00:48,940 --> 00:00:57,460
‫So it says while temp next or you could write this as while temp next is not equal to null pointer,

13
00:00:57,460 --> 00:00:59,110
‫it would be the same thing.

14
00:00:59,110 --> 00:01:06,370
‫And the conditional in this wild loop will be true if that's pointing to a node and it will be false

15
00:01:06,370 --> 00:01:13,000
‫when we get to the other end of the length list when it is equal to null pointer and then we'll say

16
00:01:13,000 --> 00:01:19,540
‫pre equals temp, which is the way it is and the first iteration of the while loop anyway.

17
00:01:19,750 --> 00:01:23,350
‫And then we'll say temp equals temp next.

18
00:01:23,680 --> 00:01:27,310
‫So now let's move this over and walk through this.

19
00:01:27,820 --> 00:01:30,250
‫So we'll say while temp next.

20
00:01:30,250 --> 00:01:37,300
‫And the conditional is true because temp next is pointing to a node we set pre equal to temp, which

21
00:01:37,300 --> 00:01:44,650
‫it is already in this iteration, and then we set temp to be equal to tip next, which moves this over

22
00:01:44,740 --> 00:01:47,860
‫and then we come up and do the while loop again.

23
00:01:47,860 --> 00:01:55,570
‫We move pre up and temp over and then we just keep iterating through the length list like this.

24
00:01:55,570 --> 00:01:58,930
‫This will be the last time we run the while loop.

25
00:01:59,380 --> 00:02:08,890
‫We move pre up and temp over, but now tip next is equal to null pointer that breaks us out of the while

26
00:02:08,890 --> 00:02:14,710
‫loop and now temp and pre are going to stay pointing to these two nodes.

27
00:02:14,980 --> 00:02:21,760
‫So I'm going to remove temp for now but we will be using temp at the end of the function.

28
00:02:22,030 --> 00:02:27,250
‫So we'll set tail to be equal to pre that does this.

29
00:02:27,400 --> 00:02:34,540
‫And that was the only thing we were using pre force while drop that out and then we'll say tail next

30
00:02:34,540 --> 00:02:42,550
‫equals null pointer that is tail next equals null pointer and that breaks that node off of the linked

31
00:02:42,550 --> 00:02:43,330
‫list.

32
00:02:43,570 --> 00:02:50,530
‫So now let's look at this code So far, we also created these two variables and now let's add this in

33
00:02:50,530 --> 00:02:52,450
‫with the rest of our code.

34
00:02:52,840 --> 00:03:00,190
‫So now we can decrement the length by one and that brings us to our other edge case for when we have

35
00:03:00,190 --> 00:03:02,770
‫one node in the linked list.

36
00:03:02,920 --> 00:03:10,450
‫So if this is the case, head and tail are both pointing to this node and because temp and pre are equal

37
00:03:10,450 --> 00:03:14,650
‫to head, they are both pointing to this node as well.

38
00:03:14,980 --> 00:03:20,770
‫So now that we've run these two lines, I'm going to remove these lines and everything above it and

39
00:03:20,770 --> 00:03:27,820
‫just focus in on this code and this will show why having one node is an edge case.

40
00:03:28,090 --> 00:03:36,520
‫So first we're going to run the while loop while temp next will temp next is equal to null pointer,

41
00:03:36,520 --> 00:03:39,520
‫which means we will not go into this while loop.

42
00:03:39,970 --> 00:03:41,890
‫So I'll remove this.

43
00:03:42,040 --> 00:03:45,160
‫And that was the only thing we're using temp for here.

44
00:03:45,160 --> 00:03:53,230
‫So I'm going to drop that out and then we say tail equals pre while tail and pre are both pointing to

45
00:03:53,230 --> 00:03:54,820
‫the same node already.

46
00:03:54,820 --> 00:04:03,520
‫So I'll remove that, then we'll set tail next to be equal to null pointer while tail next is already

47
00:04:03,520 --> 00:04:04,660
‫equal to null pointer.

48
00:04:04,660 --> 00:04:06,640
‫So this doesn't change anything.

49
00:04:07,180 --> 00:04:09,790
‫And that brings us to this line of code.

50
00:04:10,090 --> 00:04:13,630
‫Before we ran the function, the length was one.

51
00:04:13,690 --> 00:04:17,980
‫When we run this line, that gets changed to zero.

52
00:04:18,100 --> 00:04:25,000
‫But now we've run all of our code and head and tail are still pointing at that node and that is the

53
00:04:25,000 --> 00:04:26,980
‫problem that needs to be addressed.

54
00:04:27,370 --> 00:04:34,270
‫So we'll say if the length is equal to zero and I'm going to bring our code back here and point something

55
00:04:34,270 --> 00:04:43,090
‫out, because we have if length is equal to zero here and we have if length is equal to zero here.

56
00:04:43,510 --> 00:04:51,430
‫But this second, if statement happens after we decrement the length by one, which means for the second

57
00:04:51,430 --> 00:04:57,430
‫if statement we started with one node, we decrement it and then it became zero.

58
00:04:57,910 --> 00:04:59,500
‫So to solve this in.

59
00:04:59,630 --> 00:05:04,850
‫Side of this if statement will just set head and tail equal to null pointer.

60
00:05:05,390 --> 00:05:06,830
‫And that does this.

61
00:05:07,400 --> 00:05:09,410
‫And that solves that problem.

62
00:05:09,950 --> 00:05:13,460
‫Now, the only thing left to do is delete temp.

63
00:05:14,030 --> 00:05:18,670
‫So that node that we're removing, we had set temp to be equal to it.

64
00:05:18,680 --> 00:05:23,000
‫And when we say delete temp, that removes that from memory.

65
00:05:23,570 --> 00:05:27,140
‫So that is the entire delete last function.

66
00:05:27,140 --> 00:05:34,790
‫We'll look at this code in a moment and VS code and when we do will create this linked list because

67
00:05:34,790 --> 00:05:39,470
‫we want to test for three different situations that we have coded for.

68
00:05:39,500 --> 00:05:46,040
‫First, we want to make sure this works when we have two or more items in the linked list and we want

69
00:05:46,040 --> 00:05:51,740
‫to make sure it works when we have one item in the linked list and then we want to run it one more time

70
00:05:51,740 --> 00:05:54,890
‫to make sure it works when we have an empty linked list.

71
00:05:55,190 --> 00:05:58,520
‫So now let's flip over and take a look at this.

72
00:05:59,030 --> 00:06:07,340
‫So there is our delete last member function added to our linked list class there and I'll scroll up

73
00:06:07,820 --> 00:06:14,270
‫and in our main function we're creating that linked list with nodes with the value of one and two.

74
00:06:14,630 --> 00:06:20,930
‫And I'll scroll up again here and with these lines, we're just going to print out that linked list

75
00:06:21,170 --> 00:06:25,790
‫and then we're going to run delete last three different times and we're going to show what the length

76
00:06:25,790 --> 00:06:29,000
‫list looks like after each of those times.

77
00:06:29,270 --> 00:06:30,770
‫So I'll run this.

78
00:06:31,280 --> 00:06:36,350
‫And over here, this is our linked list before we have removed any items.

79
00:06:36,350 --> 00:06:39,200
‫And this is what it looks like after the first time running.

80
00:06:39,200 --> 00:06:42,860
‫Delete last and then after the second time we have an empty linked list.

81
00:06:42,860 --> 00:06:48,410
‫So we run it one more time and it is working for all three scenarios.

82
00:06:48,740 --> 00:06:54,500
‫So it looks like we have a working function for delete last.

