<jupyter_start><jupyter_text>Linked List Reversal<jupyter_code>class Node(object):
    def __init__(self,value):
        self.value = value
        self.nextnode = None<jupyter_output><empty_output><jupyter_text>SolutionSince we want to do this in place we want to make the funciton operate in O(1) space, meaning we don't want to create a new list, so we will simply use the current nodes! Time wise, we can perform the reversal in O(n) time.We can reverse the list by changing the next pointer of each node. Each node's next pointer should point to the previous node.In one pass from head to tail of our input list, we will point each node's next pointer to the previous element.Make sure to copy current.next_node into next_node before setting current.next_node to previous. Let's see this solution coded out<jupyter_code>def reverse(head):
    current = head
    previous = None
    nextnode = None

    while current:
        nextnode = current.nextnode
        current.nextnode = previous

        previous = current
        current = nextnode

    return previous<jupyter_output><empty_output><jupyter_text>Test the solution<jupyter_code># Create a list of 4 nodes
a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)

# Set up order a,b,c,d with values 1,2,3,4
a.nextnode = b
b.nextnode = c
c.nextnode = d
print (a.nextnode.value)
print (b.nextnode.value)
print (c.nextnode.value)
reverse(a)
print (d.nextnode.value)
print (c.nextnode.value)
print (b.nextnode.value)<jupyter_output>3
2
1
{'fraction_non_alphanumeric': 0.08053691275167785, 'fraction_numerical': 0.0087248322147651, 'mean_word_length': 4.037162162162162, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '222163', 'license': 'CC0-1.0', 'n_tokens_mistral': 483, 'n_tokens_neox': 468, 'n_words': 177, 'path': 'DS_Algo/Linked List/Reverse_LL.ipynb', 'repo_name': 'ShivamBhosale/100DaysOfCode_V2', 'size': '1490'}