Linked List Part 2
Tags: dsalgo
Published: 2022-05-15 08:34:16

**_Prepend_**
We will add a node to starting of a node.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnyze01b3dsgoegu2hog.png)

 Case 1:
For empty linked list, we will just add our node and set it to head and tail

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vl4bs37e0eehizy4jcv6.png)
This will turn to this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y72zuwkqc549tj080ms4.png)

Case_2:
For more than 2 nodes, we will just add that and set it to head

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8f129r209bhb57d6kk35.png)
So the code will be:
```
#Prepend something
    def prepend(self,value):
        new_node=Node(value)
        #when there is empty linked list
        if self.length==0:
            self.head=new_node
            self.tail=new_node
        else: #when there is already nodes and you want to add a node
            new_node.next=self.head
            self.head=new_node
        self.length+=1
        return True
```
**_Pop first_**

Case 1:
If we don't have any item in the list:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lclnex1086eb5wxfl93.png)

 For this, we will check if the length is 0, then returns None


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvydtrbrounszml530en.png)
 
Case 2:
If we have more than 2 values,  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0dtdku5suv25snxgp40.png)
We will remove the first value 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pbw5dg5nzios96u7jbc.png)
To remove it we need something to point at it and we will call it `temp  `


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9ne9qudnrg2q7u56kfz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qowa2uy04v11filzo4dk.png)

now, we will remove the head value to next

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1h815byjexrfjctyenbx.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cansu0hnzaju93uy1iz.png)
Now, we will remove the head 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6w4ajakl5rivbchhuag.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9yeflpshrs5ureuvefw.png)
Thus the code will be:
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/boi8bb0oynpri7a8ee5p.png)
 

Case 3:
When we have  1 item in the list. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffkqbbdriwvi7yvktz6h.png)

Now , we will assign temp to self.head

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gi5o8ut4dbx7xks45ggu.png)
Will set head.next to None

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dh7t8igh0ip4hwbmixw5.png)
Also we will decrement the list to 0 and also point tail to None


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k68je8t80oq1sc8jskxx.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pb0yra10d93zq3bdxd4c.png)
  
So, the real code is:

```
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None
        

class LinkedList:
    def __init__(self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.length = 1

    def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
        
    def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True

    def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        pre = self.head
        while(temp.next):
            pre = temp
            temp = temp.next
        self.tail = pre
        self.tail.next = None
        self.length -= 1
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp

    def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True
    
    #To pop first 
    def pop_first(self):
        #if the list has 0 values
        if self.length == 0:
            return None
        #if the list has more than 2 values or 1 values    
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp #to get the node
        #return temp.value : To get the node values


my_linked_list = LinkedList(2)
my_linked_list.append(1)

#As the LinkedIn list has 2,1 & None in the node, if we return temp.value ; we can see that we are getting 2, 1 and then None
# (2) Items - Returns 2 Node
print(my_linked_list.pop_first())
# (1) Item -  Returns 1 Node
print(my_linked_list.pop_first())
# (0) Items - Returns None
print(my_linked_list.pop_first())


```

Output (Returning the nodes)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nryjr8sagladse23b7lc.png)

**_Get_** 
We will get a value from index now. So, to start , we will check the limit of the index if it exceeds or not.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvnamouho8c0ywez1cvm.png)

So, the code will be:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bxqsa4cruygj52fifee.png)
Now assigning temp to the first value of the list and then looping through it to get the index value:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5vtahlirobwxze67t6l.png)

So, we will use

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42b6bfnglgz3qj2souun.png)
Actually , we use i in the loops when we need to use that i's value inside the loop for example :

```
for i in range(4):
    print(i)
```
but here we don't need this because we are not using the value of i. Thus using "_"

When we find our index, we will return it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34lqwyexugq4cwokwbb9.png)

So, the code will be

```
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None
        

class LinkedList:
    def __init__(self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.length = 1

    def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
        
    def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True

    def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        pre = self.head
        while(temp.next):
            pre = temp
            temp = temp.next
        self.tail = pre
        self.tail.next = None
        self.length -= 1
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp

    def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True

    def pop_first(self):
        if self.length == 0:
            return None
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp

    def get(self, index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _ in range(index):
            temp = temp.next
        return temp# returns the node
        # return temp.value : Returns the value
        
#Creating a linked List which has value 0,5,4,3,None
my_linked_list = LinkedList(0)
my_linked_list.append(5)
my_linked_list.append(4)
my_linked_list.append(3)

#Looking for value 2 in the linked List and will return the node
print(my_linked_list.get(2))


```
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r439pr2be6zukh4jey1j.png)   
Returns the node where it finds the index 2's node

**_Set value_**  
 Assume that we have a list which has 11,3,23,7 and you want to change the index 1 to 4 instead of 3.
Now, you can use the get method and look if that index 1 exists or not. If it exists, it will go into it and change the temp value to temp . Ultimately making 3 to 4 and return True

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkyzc0dtcn5j1is3vbzi.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kes7r4tpfaaqktix82u1.png)

Thus the code will be:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ing7ai6jupk6fskgp8m.png)

The code is

```
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None
        

class LinkedList:
    def __init__(self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.length = 1

    def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
        
    def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True

    def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        pre = self.head
        while(temp.next):
            pre = temp
            temp = temp.next
        self.tail = pre
        self.tail.next = None
        self.length -= 1
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp

    def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True

    def pop_first(self):
        if self.length == 0:
            return None
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp

    def get(self, index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _ in range(index):
            temp = temp.next
        return temp
        
    def set_value(self, index, value):
        temp = self.get(index)
        if temp:
            temp.value = value
            return True
        return False
    
#A linked list having 11,3,23 and 7
my_linked_list = LinkedList(11)
my_linked_list.append(3)
my_linked_list.append(23)
my_linked_list.append(7)

#changing the index 1 value to 4 instead of 3
my_linked_list.set_value(1,4)

my_linked_list.print_list() #printing the e whole list
```
   
 Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bei34ou3xeikcods4gfq.png)

**_Insert_**
In a particular index, we will add a node.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9bsz8fnl8tt9j4pby6g.png)
Firstly, we will check the index where we want to send is within the range or not .
Case 1:
Secondly, if we want to add the node 4 at the beginning of the list, it is prepending.
Case 2:
Again,if we want to add it to the end of the list , it is append
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ydsn237ctqf6zq81btr.png)
  
case 3:
If we have to insert the node within the list, 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ginrxylvx6o8t05mhrdn.png)
Assuming that we want to insert node 4, in index 2. So, we will set a temp in index 1. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nh72w8cpegis2bmry68.png)
We will point the temp.next as the node and node.next as the index 2 value which is 23.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8a0azvtq3f72x0yjl5ss.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5vmtwo67cpzemayb01x.png)
So, the code for this change

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9egnug6fgtbdtar4k2k.png)
Again, the code for this change
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eetwegiqitymm93qwd9w.png)![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3ttvtpc06jxldgmas0z.png)

Thus it is:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7e5ocbojumis3t5zvy5.png)
So, the overall code will be:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrdrxw2pusj96qy7ci0m.png)

Lets do the coding
We will turn this 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y951d3zyzbg7f5dhy5j8.png)
to this
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x03i3z1nwlmgp7jwf6ts.png)

the code:

```
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None
        

class LinkedList:
    def __init__(self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.length = 1

    def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
        
    def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True

    def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        pre = self.head
        while(temp.next):
            pre = temp
            temp = temp.next
        self.tail = pre
        self.tail.next = None
        self.length -= 1
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp

    def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True

    def pop_first(self):
        if self.length == 0:
            return None
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp

    def get(self, index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _ in range(index):
            temp = temp.next
        return temp
        
    def set_value(self, index, value):
        temp = self.get(index)
        if temp:
            temp.value = value
            return True
        return False
    
    def insert(self, index, value):
        if index < 0 or index > self.length:
            return False #if the insert method fails
        if index == 0:
            return self.prepend(value)
        if index == self.length:
            return self.append(value)
        new_node = Node(value)
        temp = self.get(index - 1)
        new_node.next = temp.next
        temp.next = new_node
        self.length += 1   
        return True  #if the insert method passes
  
# linked list having value 0 & 2
my_linked_list = LinkedList(0)
my_linked_list.append(2)

#insert value 1 in index 1
my_linked_list.insert(1,1)

my_linked_list.print_list()

```
     
**_Remove_**
Note: None is opposite of returning a node
True is opposite of returning False
Case 1:
Lets remove the index 1's node

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2n20blejljz0haxdlqe.png)
Case 2:
To remove the last item
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gh3hjcwq3fqnt277l44s.png)
Case 3:
To remove something from middle. Assume we will remove index no 2 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9yu9kx4zyxi840uszq0.png)
We are going to assign prev and temp to indexes

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvzjmu9gjko8g250lk6b.png)
Because when you remove the temp value, you need something to add that to the other part

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oregkxe8k0s1wm0f7hwr.png)
We will set prev and next by this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7salacbd3lgsoxt7frm5.png)

So, this is how things are going to work

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shytiq0gw2iy6k9trswi.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ip8rahgzkphtgszv7jw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cg7dbkgubgc48brdtuej.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aavlq6j4p3sn5tzueqsa.png)

So, lets remove the index 2 from this list

and ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2k4auxxfowzzjpyxrzw2.png)
make it this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c11h6v1adgt4vxicn3dg.png)
Code:

```
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None
        

class LinkedList:
    def __init__(self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.length = 1

    def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
        
    def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True

    def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        pre = self.head
        while(temp.next):
            pre = temp
            temp = temp.next
        self.tail = pre
        self.tail.next = None
        self.length -= 1
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp

    def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True

    def pop_first(self):
        if self.length == 0:
            return None
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp

    def get(self, index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _ in range(index):
            temp = temp.next
        return temp
        
    def set_value(self, index, value):
        temp = self.get(index)
        if temp:
            temp.value = value
            return True
        return False
    
    def insert(self, index, value):
        if index < 0 or index > self.length:
            return False
        if index == 0:
            return self.prepend(value)
        if index == self.length:
            return self.append(value)
        new_node = Node(value)
        temp = self.get(index - 1)
        new_node.next = temp.next
        temp.next = new_node
        self.length += 1   
        return True  

    def remove(self, index):
        #checking if the index exists or not
        if index < 0 or index >= self.length:
            return None
        #if need to remove the first index Case 1   
        if index == 0:
            return self.pop_first()
        #if need to remove the last index    Case 2
        if index == self.length - 1:
            return self.pop()
        #if need to remove in the middle    Case 3 
        pre = self.get(index - 1)
        temp = pre.next
        pre.next = temp.next
        temp.next = None
        self.length -= 1
        return temp
  
#Creating a list of 11,2,23,7
my_linked_list = LinkedList(11)
my_linked_list.append(3)
my_linked_list.append(23)
my_linked_list.append(7)

#removing the index 2
print(my_linked_list.remove(2))

my_linked_list.print_list()

```
**_Reverse_**  

Note: Very important for interview 
We are going to make this 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ert547zyis1xhtauum0m.png)
to


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uasf74binmxio2070k2o.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6pu1n3ha7e8g9hyd249l.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlmql07svvyk7it59qgq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inkqyl6o37e7e2ppprh1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kam1s088g7qzfze4yt9m.png)             
  
So,we will switch head and tail
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgfoqcou771ipfxlsjbp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3csv9iloq8uf1t05ohf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q61uxcpra3bhkvl4x6q7.png)
So, the codes for this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxdivvn0vuq2ahx3i0yt.png)
Now we are going to assign `before` & `after `  

T![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khutw7dltiz8tdx10mpk.png)
hey will iterate and make it reversed

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzqmlxokaorp4s59yew5.png)
Lets iterate through

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tsitlzz90eu3g96lme6.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1329ljn5tbh9hbxzs6a.png)
and now the important one

Th![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wfekt0n6fri693spxqj.png)
this change flips   temp.next to before . So, 1 flip done

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1ctyto9yop81rwodeh2.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mp2l3l8t35xhtvjbk0z5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytd60jwsfqsyzizgvy6o.png)
   
So, 1 iteration is done and this is how it will flip all the way

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb8pcdduktpucszdfwpw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndja9u0lww3a00gtpv0u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv7da7pg7c45zq046fgt.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kci0t01mqwnjcl3c119w.png)
So, again the iteration is done and thus total list will be reversed

For the last one,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05j1abpcnbfcw0k6q0d1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kij8j6q2oknp5pfrxe8r.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oy6gl13ddcdy9ro7rslh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gako4o6npdom57dku61.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bb03i4nzzs99p9tbcfw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zntvhie7sz4pkqcm1tgv.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lumas53k4ahvcdlmhzv7.png)

Done!
Let's reverse it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3utb4t0s2wwr5kls99w0.png)
            
to
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyxasqs8yaerou8lgh7l.png)

Code

```
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None
        

class LinkedList:
    def __init__(self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.length = 1

    def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
        
    def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True

    def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        pre = self.head
        while(temp.next):
            pre = temp
            temp = temp.next
        self.tail = pre
        self.tail.next = None
        self.length -= 1
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp

    def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True

    def pop_first(self):
        if self.length == 0:
            return None
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp

    def get(self, index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _ in range(index):
            temp = temp.next
        return temp
        
    def set_value(self, index, value):
        temp = self.get(index)
        if temp:
            temp.value = value
            return True
        return False
    
    def insert(self, index, value):
        if index < 0 or index > self.length:
            return False
        if index == 0:
            return self.prepend(value)
        if index == self.length:
            return self.append(value)
        new_node = Node(value)
        temp = self.get(index - 1)
        new_node.next = temp.next
        temp.next = new_node
        self.length += 1   
        return True  

    def remove(self, index):
        if index < 0 or index >= self.length:
            return None
        if index == 0:
            return self.pop_first()
        if index == self.length - 1:
            return self.pop()
        pre = self.get(index - 1)
        temp = pre.next
        pre.next = temp.next
        temp.next = None
        self.length -= 1
        return temp

    def reverse(self):
        #switching  head and tail
        temp = self.head
        self.head = self.tail
        self.tail = temp
        after = temp.next
        before = None
        
        #reversing 
        for _ in range(self.length):
            after = temp.next
            temp.next = before
            before = temp
            temp = after
  
#Created a linked List of 1,2,3,4
my_linked_list = LinkedList(1)
my_linked_list.append(2)
my_linked_list.append(3)
my_linked_list.append(4)

#making it reversed
my_linked_list.reverse()

my_linked_list.print_list()
```
Output
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1iah995yomgg2637enq.png)