 Hello everyone, my name is Nikhil Gajam, working as assistant professor in department of computer science and engineering in Valtran Institute of Technology, Solapur. In today's session we will see a program for reversing singly linked list, a learning outcome at the end of this session student will be able to write a C program for reversing singly linked list. Now this is a singly linked list, a linked list can be defined as a collection of nodes where each node contains two fields a data and pointer which points to a next node. We are talking about singly linked list, in doubly linked list there are three nodes in general data and two pointers, one pointer will point to next node and other pointer will points to previous node. So this is an example of singly linked list, suppose there are four nodes 10, 20, 30, 40, these are actual values stored in data part and this is the next part, each nodes next part will store address of next node, so next node address is 200, 200 is stored in next part of node 10, like this a singly linked list is created, last node's next part will always contains null indicating that this is the end of singly linked list. Then the pointer we are using called as start, start is a pointer to a structure which holds address of first node, so first node address is suppose 100, so 100 is stored in start, so start always points to first node, this is singly linked list. Now difficulty in reversing singly linked list, there are two concept, printing singly linked list in reverse direction this is different and actual reversing singly linked list is different. Now when we are talking about singly linked list, there is only one pointer which points to a next node, so traversing singly linked list is possible only in forward direction, reverse direction traversal is not possible in case of singly linked list, in case of doubly linked list this is possible to traverse in both the direction forward and reverse, but in case of singly linked list it is not possible to traverse in reverse direction, so this is a difficulty arise in singly linked list, so reversing singly linked list is difficult as compared with reversing doubly linked list. Now these are two concepts in case with singly linked list, displaying singly linked list is reverse order, suppose this is my singly linked list containing four nodes 10, 20, 30, 40, so after execution of this function output will be 30, so 40, 30, 20 and 10, in this case you are just printing singly linked list in reverse direction, so output will be 40, 30, 20, 10, so after this function singly linked list, original singly linked list will remain as it is. In case of reversing singly linked list, suppose this is my original linked list containing 10, 20, 30, 40, so after reversing singly linked list the original linked list will become like this, 40, 30, 20 and 10, start will point to a 40 after reversing singly linked list, in original linked list start is pointing at node 10, so this is actual reversing of singly linked list, so 40's next part will hold address of node 30, 30's next part will hold address of node 20, 20's next part will hold address of 10, 10's next part contains null indicating this is end of singly linked list. Now what is an approach for reversing actual singly linked list? This is a recursive approach I am using here, I am using two pointer variable temporary variable apart from start, start is a global variable which always points to a first node, apart from start I am using two pointers current and previous, so current again it is a pointer to a structure is used for pointing to a current node, previous node pointer, previous pointer is actually used for pointing to a previous node, traverse current and previous pointer till it reaches to end, then go on storing address of previous in current arrow next part, then start point to a previous at last, so at last start should point to a previous pointer, we will see by example suppose this is my original linked list containing 4 nodes 10, 20, 30 and 40, suppose address of node 10 is 100, address of node 20 is 200, address of node 30 is 300 and address of node 40 is 400, 10's next stores address of node 20 that is 200, so 200 is stored in 10 arrow next, 300 is stored in 20's next, 400 is stored in 40's next and last nodes next will hold null indicating end of linked list, so start will always points to a first node that is 10, so start will have value 100 which is address of first node, so initially current is pointing to a first node and previous is pointing to a null, so current indicating or pointing to a current node and previous pointer will point to a previous node, there is no previous node that is why previous pointer will have initially null value, will traverse previous and current till these two values reaches to an end, end of linked list, so current will move forward and previous will point to previous of current node, so previous will point to 10 and current will point to 20, will move forward, now current will move forward, current will point to a 30 node, previous pointer will point to a 20 node, again current will move forward, current will point to a 40 node, previous will point to a 30 node, again will move forward, current will point to a null and previous will point to a node 40, when current pointer will have null value in that case we will stopping or this is end of traversing first part we are doing is start will point to a previous. So after reversing 40 will become the first node that is why start will point to a previous which is 40. Using recursive we are using recursive approach. So recursive function will return to a previous call from where it is called. So current arrow next now will hold the address of previous. So in this way every current values next part will hold address of previous. Because 40 will become the first node next node will be 30. So 40's next will hold the address of 30. So 30 is nothing but previous. So current arrow next will go on holding address of previous value like this. Now current next will hold address of previous so 300 so 200 will be hold in current next then current next will hold address of previous so 100 will be stored in current arrow next. Now current arrow next 10's next will hold previous value is null. So null is stored in 10 arrow next like this wise 10 will become last node 40 will become first node and start will point to a 40. So this is an approach for reversing singly linked list. You can pause the video and you can answer what will be the value in pointer part of first node after reversing singly linked list. You can pause the video and you can answer. Answer for this is null. Originally 10 is the first node. So after reversing singly linked list first nodes next will hold null value. So this is the answer. This is the program or function for reversing singly linked list will execute this code just a second. So this is online compiler I am using. So in this program I have written a code for creating singly linked list and displaying in forward direction. So this is a main this is a main node driven program I have two functions insert at end and display. So this is a display code and this is insert a new node at the end and will execute this code first and I am checking whether this program is working correctly or not language I have used is C enter choice 1 suppose enter data to be inserted 10 enter choice 1 20 30 suppose I am displaying now. So this is displaying singly linked list in forward direction. I will execute this code and I will check whether this function or this approach is working correctly for this program or not. So this is the function reverse linked list. It accepts two arguments first one is previous and second one is current these are two variables data type of this variable is pointer to structure. Now I am calling recursive function reverse linked list I am passing two values current and current arrow next this is for traversing current and previous pointers till current value reaches to null and at the end start will point to previous. So this is a function I will add another menu case 3 for reversing singly linked list. So this is reverse linked list initially I am passing null value for previous and start value for current while choice is not equal to 4. Decrease this function while reverse this is struct pointer. I will execute this code now this is run enter data 10 I will display this is display of singly linked list. So linked list is created 10 20 30 now choice 3 is for reversing singly linked list I will enter choice 3. So after reversing I am displaying now this is display of singly linked list. So after reversing now 30 will become the first node and 10 will become the last node originally 10 is my first node and 30 was my last node. So after reversing 30 will become a first node and 10 will become a last node. So this is a code or function for reversing singly linked list I will shift again for this PPT presentation. So this is a function or program for reversal of singly linked list these are the references I have used for creation of this video. Thank you.