MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / reverse

Method reverse

data_structures/linked_list/singly_linked_list.py:337–363  ·  view source on GitHub ↗

This reverses the linked list order. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third >>> linked_lis

(self)

Source from the content-addressed store, hash-verified

335 return self.head is None
336
337 def reverse(self) -> None:
338 """
339 This reverses the linked list order.
340 >>> linked_list = LinkedList()
341 >>> linked_list.insert_tail("first")
342 >>> linked_list.insert_tail("second")
343 >>> linked_list.insert_tail("third")
344 >>> linked_list
345 first -> second -> third
346 >>> linked_list.reverse()
347 >>> linked_list
348 third -> second -> first
349 """
350 prev = None
351 current = self.head
352
353 while current:
354 # Store the current node's next node.
355 next_node = current.next_node
356 # Make the current node's next_node point backwards
357 current.next_node = prev
358 # Make the previous node be the current node
359 prev = current
360 # Make the current node the next_node node (to progress iteration)
361 current = next_node
362 # Return prev in order to put the head at the end
363 self.head = prev
364
365
366def test_singly_linked_list() -> None:

Callers 15

test_singly_linked_listFunction · 0.95
mainFunction · 0.95
is_luhnFunction · 0.80
merge_sortFunction · 0.80
retrace_pathMethod · 0.80
show_pathMethod · 0.80
dijkstraFunction · 0.80
searchFunction · 0.80
retrace_pathMethod · 0.80
bidirectional_searchFunction · 0.80

Calls

no outgoing calls

Tested by 2

test_singly_linked_listFunction · 0.76