Question: Implement the LinkedList method 'swap_front_back' for LinkedList and LinkedListNode class. from typing import Union, Any class LinkedListNode: def __init__(self, value: object, next_: Union[LinkedListNode, None] =
Implement the LinkedList method 'swap_front_back'
for LinkedList and LinkedListNode class. from typing import Union, Any class LinkedListNode: def __init__(self, value: object, next_: Union["LinkedListNode", None] = None) -> None: >>> LinkedListNode(3).value 3 >>> LinkedListNode(3).next_ == None True """ self.value = value self.next_ = next_ def __str__(self) -> str: """ Return a string representation of this LinkedListNode. >>> print(LinkedListNode(3)) 3 -> """ return "{} -> ".format(self.value) class LinkedList: def __init__(self) -> None: """ Initialize an empty LinkedList. >>> lnk = LinkedList() >>> lnk.size 0 """ self.front = None self.back = None self.size = 0 def prepend(self, value: Any) -> None: """ Insert value to the start of this LinkedList (before self.front). >>> lnk = LinkedList() >>> lnk.prepend(0) >>> lnk.prepend(1) >>> print(lnk) 1 -> 0 -> | """ self.front = LinkedListNode(value, self.front) if self.back is None: self.back = self.front self.size += 1 def __str__(self) -> str: """ Return a string representation of this LinkedList. >>> lnk = LinkedList() >>> lnk.prepend(0) >>> lnk.prepend(1) >>> print(lnk) 1 -> 0 -> | """ cur_node = self.front result = '' while cur_node is not None: result += str(cur_node) cur_node = cur_node.next_ return result + '|' Exercise code def swap_front_back(self) -> None: """ Swap the LinkedListNodes of the front and back of this linked list. Do not create any new LinkedListNodes. Do not swap the values of the LinkedListNodes. >>> lnk = LinkedList() >>> lnk.prepend(3) >>> lnk.prepend(2) >>> lnk.prepend(1) >>> print(lnk) 1 -> 2 -> 3 -> | >>> front_id = id(lnk.front) >>> back_id = id(lnk.back) >>> lnk.swap_front_back() >>> print(lnk) 3 -> 2 -> 1 -> | >>> front_id == id(lnk.back) True >>> back_id == id(lnk.front) True """ implement code here Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
