1
00:00:00,770 --> 00:00:01,700
Hi everyone.

2
00:00:01,700 --> 00:00:03,590
Let's do this question now.

3
00:00:03,590 --> 00:00:06,710
This question is about designing a singly linked list.

4
00:00:06,710 --> 00:00:07,790
So let's read it.

5
00:00:07,790 --> 00:00:12,020
Design a singly linked list class that has a head and tail.

6
00:00:12,020 --> 00:00:18,230
Every node is to have two attributes value the value of the current node, and next a pointer to the

7
00:00:18,230 --> 00:00:18,950
next node.

8
00:00:18,950 --> 00:00:21,050
The linked list is to be zero indexed.

9
00:00:21,050 --> 00:00:22,970
The class should support the following.

10
00:00:22,970 --> 00:00:23,960
So that's the question.

11
00:00:23,960 --> 00:00:25,700
Now the class should support the following.

12
00:00:25,700 --> 00:00:28,220
The first is we have singly linked list over here.

13
00:00:28,220 --> 00:00:31,100
Now this initializes the singly linked list object.

14
00:00:31,100 --> 00:00:35,420
So we have seen previously that a class is a blueprint for creating objects.

15
00:00:35,420 --> 00:00:37,070
So it's defining a pattern.

16
00:00:37,070 --> 00:00:40,880
And objects can have uh parameters and methods.

17
00:00:40,880 --> 00:00:42,110
So we have seen that before.

18
00:00:42,110 --> 00:00:42,470
All right.

19
00:00:42,470 --> 00:00:46,670
So again we will just have a class and its name will be singly linked list.

20
00:00:46,670 --> 00:00:52,550
And then we can just say uh const um singly linked list one is equal to new singly linked list.

21
00:00:52,550 --> 00:00:55,100
So that's how we will create this object over here.

22
00:00:55,100 --> 00:00:55,460
All right.

23
00:00:55,460 --> 00:00:57,530
So this is going to be pretty straightforward.

24
00:00:57,530 --> 00:00:58,790
And we have seen this before.

25
00:00:58,790 --> 00:01:01,910
And we will again visit this when we code out our solution.

26
00:01:01,910 --> 00:01:04,010
Now let's read the remaining part over here.

27
00:01:04,010 --> 00:01:08,240
It's mentioned that some instance methods should be written for this particular class.

28
00:01:08,240 --> 00:01:11,450
So the first instance method is get index.

29
00:01:11,450 --> 00:01:13,610
Now over here the index is passed to it.

30
00:01:13,610 --> 00:01:16,670
Now this should get the value of the index node.

31
00:01:16,670 --> 00:01:19,250
If the index is invalid return minus one.

32
00:01:19,250 --> 00:01:22,070
So for example if our singly linked list is something like this.

33
00:01:22,070 --> 00:01:24,020
So we have one pointing to two.

34
00:01:24,020 --> 00:01:26,630
And then we have this pointing to a node with value three.

35
00:01:26,630 --> 00:01:27,860
And this points to null.

36
00:01:27,860 --> 00:01:29,240
So this is a singly linked list.

37
00:01:29,240 --> 00:01:33,830
Now if we pass index one then it should get this particular node over here.

38
00:01:33,830 --> 00:01:38,030
Again this is index zero because it's mentioned that the linked list is to be zero indexed.

39
00:01:38,030 --> 00:01:38,840
So this is zero.

40
00:01:38,840 --> 00:01:40,220
This is one and this is two.

41
00:01:40,220 --> 00:01:45,980
And again remember we cannot do this in constant time as in the case of an array because the indices

42
00:01:45,980 --> 00:01:48,980
are not stored um in inbuilt manner.

43
00:01:48,980 --> 00:01:49,220
Right.

44
00:01:49,220 --> 00:01:52,610
So we'll have to traverse and we'll have to use a count variable for that.

45
00:01:52,610 --> 00:01:52,970
All right.

46
00:01:53,000 --> 00:01:54,710
Now let's continue reading the question.

47
00:01:54,710 --> 00:01:59,480
The next instance method is add add head and a value is passed to it.

48
00:01:59,870 --> 00:02:01,310
So a value would be passed.

49
00:02:01,310 --> 00:02:05,690
And we have to add a node of the given value before the first element of the linked list.

50
00:02:05,690 --> 00:02:11,060
So for example, if the value four is passed and added head instance method is called for this singly

51
00:02:11,060 --> 00:02:15,920
linked list, then we would have to make a node with value four and that should be added at the head.

52
00:02:16,400 --> 00:02:18,590
So that's the second instance method over here.

53
00:02:18,590 --> 00:02:21,470
Now the next one is add a tail and a value is passed.

54
00:02:21,470 --> 00:02:24,350
So again in a similar manner if we want to add five.

55
00:02:24,350 --> 00:02:25,790
So we would have to add it over here.

56
00:02:25,790 --> 00:02:29,150
So this three will be pointing to five which will point to null.

57
00:02:29,150 --> 00:02:33,410
So add a node of given value at the last element of the linked list.

58
00:02:33,440 --> 00:02:40,130
Now the next instance method is added index and index and value is passed to add a node of given value

59
00:02:40,130 --> 00:02:43,250
before the index node in the linked list.

60
00:02:43,250 --> 00:02:48,500
If the index equals the length of the linked list, the node will be appended to the end of the linked

61
00:02:48,500 --> 00:02:48,800
list.

62
00:02:48,800 --> 00:02:52,190
If index is greater than the length, don't insert the node.

63
00:02:52,190 --> 00:02:53,720
So this is very clear.

64
00:02:53,720 --> 00:02:58,910
So for example in this linked list over here let's write the indices zero, one two and three.

65
00:02:58,910 --> 00:03:04,640
So if the index is equal to four over here when we call this function then we will add it at the end

66
00:03:04,640 --> 00:03:05,240
as the tail.

67
00:03:05,240 --> 00:03:07,700
So it's mentioned over here if index equals the length.

68
00:03:07,700 --> 00:03:09,230
So the length over here is four.

69
00:03:09,230 --> 00:03:11,840
The node will be appended to the end of the linked list.

70
00:03:11,840 --> 00:03:14,900
If index is greater than the length for example over here the length is four.

71
00:03:14,900 --> 00:03:17,360
And index let's say is 5 or 10.

72
00:03:17,360 --> 00:03:19,220
In that case we won't insert the node.

73
00:03:19,220 --> 00:03:20,600
So that's what is mentioned over here.

74
00:03:20,600 --> 00:03:25,610
And then if we want to insert a node at index one, it's going to be inserted before this.

75
00:03:25,610 --> 00:03:29,000
So that the new node let's say that's value that has value five.

76
00:03:29,000 --> 00:03:29,750
The new node.

77
00:03:29,750 --> 00:03:33,470
Once it is inserted over here it will be at index one.

78
00:03:33,800 --> 00:03:35,180
So that is what is mentioned over here.

79
00:03:35,180 --> 00:03:39,410
Add a node of given value before the index node in the linked list.

80
00:03:39,410 --> 00:03:42,440
So that is added index function or method over here.

81
00:03:42,440 --> 00:03:45,830
And then the last instance method is delete at index.

82
00:03:45,830 --> 00:03:47,570
And we only pass the index.

83
00:03:47,570 --> 00:03:51,590
So delete the index node in the linked list if the index is valid.

84
00:03:51,590 --> 00:03:52,820
Else nothing happens.

85
00:03:52,820 --> 00:03:58,700
So for example, if this is the linked list and we want to delete this particular node, then this will

86
00:03:58,700 --> 00:03:59,150
be removed.

87
00:03:59,150 --> 00:04:00,800
And this will point to the next node.

88
00:04:00,800 --> 00:04:03,260
So these are the instance methods that we have to write.

89
00:04:03,260 --> 00:04:07,550
So let's go ahead and see in the next video how we can solve this question.
