• 15 hours ago
Transcript
00:00Next time we talk about add last key
00:01Assume that I have this link list
00:04Now I want to add the node to last value
00:08I will perform operations with the tail
00:12First step is to add a node
00:14So I will create that node
00:16If I want to add a node
00:17Then I will create it
00:20Step 1 is to create a node
00:24Can I call it a new node?
00:28Yes
00:28What will be the second step?
00:31The second step is to add a node
00:33Because my node is going to create last
00:37So the tail is going to point null
00:40Now I will point null
00:43Because my link list should be like this
00:46Where every node points its next node
00:49Because my new node is going to come last
00:52So the last node is going to point it
00:58To point the next node is going to point it
01:09Now I will point it
01:10Now I will point the next node
01:13What will I do now?
01:14To point the next node
01:15To point the next node
01:15Now I will point the next node
01:27To point the next node
01:27And the next node is going to point null
01:32And the next node is going to point null
01:32So what is the third step?
01:33The third step is to assign the tail value to the tail
01:39And the next node is going to point the next node
01:40I will summarize it
01:41First I will create a new node
01:44And the next node will be going to point the next node
01:48To point the next node
01:48And the next node will be going to add the tail
01:51So I will add any node in the last node
02:00To point the next node
02:07To point the next node
02:09To point the next node
02:20To point the next node
02:21So this will be a special case
02:23For the next node
02:24I will perform these 3 steps
02:27Which I have written
02:28Now we are going to write code
02:30To write code
02:31First I will create my function
02:34Public
02:34Void
02:35Add
02:36Last
02:37Add last
02:37I will have single parameter
02:39Which is my data
02:40Now I have to perform the same steps
02:42Step 1 was to create a new node
02:45So I will write node
02:46New node
02:47Is equal to
02:48New node
02:49Of
02:50Data
02:50I have written the same line
02:52Which I have written in add first
02:54Now my second step was to point the next tail
02:57To point the next node
02:58To my new node
02:59So my tail
03:01.next
03:02Is equal to
03:03My new node
03:04And the third step was to point the next node
03:06To my new node
03:07To my new node
03:08So
03:09Tail is equal to
03:11New node
03:13And here
03:14My special case will come here
03:16That if my head
03:17Is equal to null
03:19That means my link list is empty
03:21Here I can equate the tail
03:24But by default
03:24Or by convention
03:25If my head is null
03:28Then we believe the link list
03:30So if my head is null
03:31What do I have to do
03:33In that case
03:33My head will equal
03:35Tail will equal
03:37New node
03:38That means
03:38I have given both
03:39New node
03:40And now
03:41I will do
03:42Return
03:43So
03:44This is the whole function
03:46For my add last
03:46Now
03:47If
03:48I will add last
03:50Here
03:50I will do
03:512
03:51And 1
03:53And now
03:54I will do
03:54Linklist.addlast
03:573
03:57And
03:58I will do
03:59Linklist.addlast
04:024
04:05So
04:05How will my link list
04:07Create
04:08One
04:08We will try to run
04:10In the beginning
04:11In the beginning
04:11In the beginning
04:12My link list was empty
04:13In the beginning
04:14So what was my head
04:15Now
04:16What was null
04:17As I called
04:18Add first
04:19To
04:20One node
04:22What data
04:23Added in the node
04:24And
04:25Next
04:25Now
04:26I will point
04:26My null value
04:28My head
04:29And tail
04:30After
04:31When I called
04:32Add first
04:35One
04:35Call
04:36I have a new node
04:37Create
04:37In the first position
04:38Which
04:40Next
04:40Now
04:41I will point
04:41To my head
04:42And my head
04:44Shift
04:44To
04:46First node
04:47After
04:48I called
04:49Add last
04:50So
04:51The new node
04:52Create
04:52In my last
04:54Position
04:54It will create
04:56Three
04:56As my data
04:58And
04:58Next
04:59Now
04:59Because
05:01This is created
05:01In the last
05:02So
05:03First
05:04I will point
05:07From null
05:08To
05:09My last node
05:11And
05:12Tail
05:13Shift
05:14To
05:15This
05:15New node
05:16After
05:17I have to add
05:18Other
05:18Data
05:18To
05:18Add
05:18Then
05:19A new node
05:20Last
05:21Will create
05:22In the last node
05:23And
05:23Next
05:24It will point
05:25To my
05:31New node
05:32So
05:32From null
05:33It will point
05:34To my
05:36New node
05:36And
05:37Tail
05:37Shift
05:38From
05:39This node
05:39To
05:40My last node
05:42So
05:42I can say
05:45That
05:46That
05:46To
05:47That
05:47That
05:47It will
05:47Know
05:47where
05:47Data
05:481
05:49To
05:51point
05:512
05:52Then
05:52Point
05:532
05:53Then
05:533
05:53Then
05:54Point
05:54Then
05:54Finally
05:56NULL
05:57Head
06:03And
06:04And if we look at the add last function properly,
06:08then I will know that I am performing constant operations.
06:12This is a constant time operation.