1 view
in Python

I'm approached to reverse a which accepts head as the parameter where as head is a connected linked list e.g.: 1 - > 2 - > 3 which was gotten back from a capacity previously characterized I attempted to execute the function reverse_linked_list thusly:

temp1 = temp.next

temp2 = temp1.next

temp1.next = None

temp2.next = temp1

temp1.next = temp

return temp2

class Node(object):

def __init__(self,value=None):

self.value = value

self.next = None

prev = None

for element in plist:

node = Node(element)

else:

prev.next = node

prev = node

result = []

counter = 0

while head and counter < 100: # tests don't use more than 100 nodes, so bail if you loop 100 times.

counter += 1

return result

def check_reversal(input):

It is brought along these lines: check_reversal([1,2,3]). The function I have composed for switching the list is giving [3,2,1,2,1,2,1,2,1] and turns out just for a list of length 3. How might I sum it up for a list of length n?

by (26.4k points)

You can utilize mod function to get the remainder of every cycle (Iteration) and clearly, it will helps in reversing the list.

prev=None

for i in range(len):

node=Node(number%10)

else:

prev.next=node

prev=node

number=number/10

Interested to learn python in detail? Come and Join the python course.