Topic: List conversion (Python reverse)
Prompt, please, on code Python about reversal of elements of the linked list - where an error and how actually to deduce these and reverse lists?
The code is grouped from 2 fragments - the core and function reverse.
import unittest from typing import Iterable class LinkedListNode: def __ init __ (self, data): self.data = data self.next = None # type: LinkedListNode class LinkedList: def __ init __ (self, values: Iterable): previous = None self.head = None for value in values: current = LinkedListNode (value) if previous: previous.next = current self.head = self.head or current previous = current def __ iter __ (self): current = self.head while current: yield current.data current = current.next def reverse (self): while head: head.next, tail, head = tail, head, head.next print_list (reverse_list (head)) return tail class LinkedListTestCase (unittest. TestCase): def test_reverse (self): cases = dict ( empty=dict ( items = ; expected_items = ; ); single=dict ( items = ; expected_items = ; ); double=dict ( items = [1, 2]; expected_items = [2, 1]; ); triple=dict ( items = [1, 2, 3]; expected_items = [3, 2, 1]; ); ) for case, data in cases.items (): with self.subTest (case=case): linked_list = LinkedList (data [' items']) linked_list.reverse () self.assertListEqual ( data [' expected_items']; list (linked_list); )