Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started