1 00:00:01,930 --> 00:00:02,650 Hello, everyone. 2 00:00:02,680 --> 00:00:07,840 So today we will solve the problem and the name of the problem is reverse Elementalist. 3 00:00:07,870 --> 00:00:10,750 OK, so this is our original list. 4 00:00:12,930 --> 00:00:18,940 So the original Linklaters one, two, three, four and five, and we have to reverse this original 5 00:00:18,940 --> 00:00:19,400 linked list. 6 00:00:19,440 --> 00:00:24,100 So after reversing the original link list, this will be our answer. 7 00:00:24,120 --> 00:00:25,920 Five, four, three, two and one. 8 00:00:25,980 --> 00:00:31,380 OK, so this is our input and this should be our output. 9 00:00:31,500 --> 00:00:34,550 OK, we have to basically reverse a long list. 10 00:00:34,950 --> 00:00:37,640 OK, so how we can reverse a list. 11 00:00:37,680 --> 00:00:39,580 So this is very easy problem. 12 00:00:39,600 --> 00:00:42,340 OK, so suppose this is the scenario. 13 00:00:42,900 --> 00:00:46,200 OK, so these are the nodes of a long list. 14 00:00:48,140 --> 00:00:52,650 OK, and suppose we have reversed some part of the linked list. 15 00:00:52,670 --> 00:00:55,760 OK, so let's say the name of this node is previous node. 16 00:00:58,470 --> 00:01:06,690 The name of this note is current law and let's say the name of this notice and OK, and this list goes 17 00:01:06,690 --> 00:01:06,900 on. 18 00:01:09,040 --> 00:01:15,850 OK, so here is Nell, OK, so let us assume we have already reversed this part of the linked list. 19 00:01:16,030 --> 00:01:19,750 OK, now we have to reverse the remaining part of the linked list. 20 00:01:19,970 --> 00:01:28,030 OK, so what we will do here is, first of all, we have three point previous, current and OK, so 21 00:01:28,030 --> 00:01:30,400 we have three points previous, current. 22 00:01:30,730 --> 00:01:36,100 And so the first step, first step, whatever what we will do, we will save this node. 23 00:01:36,130 --> 00:01:38,620 OK, I have to save this node. 24 00:01:39,160 --> 00:01:39,580 So. 25 00:01:41,510 --> 00:01:42,410 North Star in. 26 00:01:43,960 --> 00:01:47,710 What is in this is basically the next of the current A.. 27 00:01:48,670 --> 00:01:55,390 OK, so I created this point that and now what we have to do so after reversing what should happen, 28 00:01:55,390 --> 00:01:56,530 this current pointer. 29 00:01:58,120 --> 00:01:59,250 Should point towards this. 30 00:01:59,390 --> 00:02:04,210 OK, so the second step is very simple, the next of current. 31 00:02:05,420 --> 00:02:12,620 It should point towards the previous OK, and this node, this link will get broke, this link will 32 00:02:12,620 --> 00:02:13,280 get broken. 33 00:02:13,310 --> 00:02:20,960 OK, so this is our second step and our link will get broken at this point. 34 00:02:21,410 --> 00:02:27,020 OK, now, if we have to do so, previous will become current. 35 00:02:27,770 --> 00:02:30,730 OK, so this previous will come here. 36 00:02:31,440 --> 00:02:34,280 OK, now previous is pointing towards current. 37 00:02:35,240 --> 00:02:42,000 Then we will do current equals and OK so now current is pointing towards here. 38 00:02:42,050 --> 00:02:45,800 OK, so this is current now and we have reversed. 39 00:02:46,980 --> 00:02:54,300 This part of the linked list, OK, again, we will we will put discord inside a loop. 40 00:02:55,500 --> 00:02:56,700 OK, so what will happen? 41 00:02:56,730 --> 00:03:00,960 So this is current, this is previous and this will be over. 42 00:03:00,960 --> 00:03:06,400 And again, we will break this link and we will make this link again. 43 00:03:06,450 --> 00:03:07,890 This will become previous. 44 00:03:08,160 --> 00:03:12,390 This will become current and it will go on, OK? 45 00:03:12,570 --> 00:03:13,550 It will continue. 46 00:03:14,170 --> 00:03:19,110 Now I am repeating one more time, so let's say. 47 00:03:22,530 --> 00:03:24,150 This is my previous Naude. 48 00:03:26,860 --> 00:03:28,420 This is my current north. 49 00:03:29,690 --> 00:03:31,940 And this is my next nolde. 50 00:03:33,060 --> 00:03:34,040 And let's see. 51 00:03:35,220 --> 00:03:38,800 There's one more note and then the end of the linked list. 52 00:03:38,900 --> 00:03:39,210 OK. 53 00:03:41,240 --> 00:03:47,570 So the first step, we have to save this node, so for saving this node, I am creating appointer. 54 00:03:47,570 --> 00:03:50,810 So Node started in which is basically the next of current. 55 00:03:51,830 --> 00:03:53,360 OK, we have to save this node. 56 00:03:53,810 --> 00:03:56,940 We have to save this node because we are going to break this link. 57 00:03:57,200 --> 00:04:01,620 Now, the second step is break this link and make this link. 58 00:04:02,270 --> 00:04:03,110 So basically. 59 00:04:04,970 --> 00:04:05,990 Next of current. 60 00:04:07,100 --> 00:04:09,300 It's pointing towards the previous. 61 00:04:09,320 --> 00:04:18,610 OK, the next of current is pointing towards the previous node, then I will make previous here, OK, 62 00:04:18,620 --> 00:04:21,980 previous will point towards the current node. 63 00:04:22,430 --> 00:04:23,630 OK, so. 64 00:04:24,790 --> 00:04:29,800 This is previous now and then what we have to do current will come here. 65 00:04:29,990 --> 00:04:32,010 OK, so this will be current. 66 00:04:32,410 --> 00:04:36,790 So current is basically an OK, so this end that we have saved. 67 00:04:37,240 --> 00:04:40,760 So current is basically this OK. 68 00:04:41,140 --> 00:04:44,650 Then again, we will we will execute the same code. 69 00:04:45,430 --> 00:04:51,030 OK, so this is current, this is and this link will get broken. 70 00:04:51,430 --> 00:04:56,050 I will make this link then it will become previous. 71 00:04:56,470 --> 00:04:57,790 It will become current. 72 00:04:58,330 --> 00:05:01,780 Then this will be end basically and will be null. 73 00:05:02,230 --> 00:05:11,590 And then I will make I will break this link then it will pointier current next next of current is pointing 74 00:05:11,590 --> 00:05:17,810 towards the previous then previous will come here and then current equals and was null. 75 00:05:17,830 --> 00:05:19,810 So basically the current will become null. 76 00:05:20,290 --> 00:05:20,720 OK. 77 00:05:20,980 --> 00:05:22,630 So when the current becomes null. 78 00:05:23,800 --> 00:05:28,430 So I will make the condition of the loop while current is not external. 79 00:05:29,200 --> 00:05:33,700 So when current becomes null that means mailing list is reversed. 80 00:05:33,820 --> 00:05:35,620 And this is our head. 81 00:05:36,550 --> 00:05:39,250 OK, this is our head of the reverse blacklist headers. 82 00:05:39,250 --> 00:05:42,310 Basically nothing at this previous only, OK? 83 00:05:43,030 --> 00:05:43,960 It is basically nothing. 84 00:05:43,960 --> 00:05:44,720 It is previous. 85 00:05:45,130 --> 00:05:46,660 So after this loop. 86 00:05:48,400 --> 00:05:51,100 Our head is nothing but previous. 87 00:05:51,700 --> 00:05:54,970 OK, now what was the initial condition so initially? 88 00:05:56,120 --> 00:05:57,620 So now let's say. 89 00:06:00,200 --> 00:06:08,120 This is our original list, so basically current is nothing but head, this is previous, which is basically 90 00:06:08,120 --> 00:06:08,380 nil. 91 00:06:08,870 --> 00:06:12,650 So previous will be null initially, OK, and this will be our end. 92 00:06:13,260 --> 00:06:15,830 OK, now this question is present. 93 00:06:15,830 --> 00:06:20,910 Only told, OK, that it was linked list discussion is present to the record. 94 00:06:20,930 --> 00:06:22,280 And now let us write the code. 95 00:06:22,640 --> 00:06:24,890 OK, so we need three pointers. 96 00:06:25,310 --> 00:06:27,680 OK, we need three pointer. 97 00:06:28,400 --> 00:06:35,150 Previous, current and and so current pointer which is held initially. 98 00:06:36,370 --> 00:06:36,910 We need. 99 00:06:37,740 --> 00:06:38,880 Our previous point that. 100 00:06:40,090 --> 00:06:41,380 Which is basically nil. 101 00:06:43,820 --> 00:06:47,750 And we need appointer and OK, so first. 102 00:06:51,750 --> 00:06:56,100 Let us ride the loop, so while current is not the questionable. 103 00:07:00,510 --> 00:07:05,150 But they have to do we have to do four steps first, save the next node, OK? 104 00:07:09,030 --> 00:07:14,370 First step, save the next order, because the next node, because we are going to break the link in 105 00:07:14,370 --> 00:07:20,880 the next lane, so I have saved the next node and now we can break the link. 106 00:07:21,100 --> 00:07:23,790 OK, so the next of clarinettist previous. 107 00:07:25,620 --> 00:07:27,090 Now we have to move ahead. 108 00:07:27,350 --> 00:07:30,960 OK, so for moving ahead, the current. 109 00:07:32,350 --> 00:07:41,410 And current equals next, basically, and OK, next month, and and when the loop over, when the loop 110 00:07:41,410 --> 00:07:47,710 is finished, basically the head of the basically the head of the reverse linked list is nothing but 111 00:07:47,710 --> 00:07:48,210 previous. 112 00:07:48,220 --> 00:07:51,960 So we will return previous and that's all OK. 113 00:07:56,060 --> 00:08:02,090 Now, let's amid the cold, so what is the time and the space complexity of our solution so the time, 114 00:08:02,090 --> 00:08:08,030 complexities, linear and the space complexities of an OK, so. 115 00:08:10,230 --> 00:08:16,700 Ben, complexity is linear and space's constant all in space. 116 00:08:16,770 --> 00:08:18,840 OK, so this was very easy problem. 117 00:08:19,600 --> 00:08:20,700 OK, thank you.