Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the LinkedList method \'swap_front_back\' by using provided LinkedList

ID: 3902314 • Letter: I

Question

 Implement the LinkedList method 'swap_front_back'
 by using provided 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         """         CODE HERE!   

Explanation / Answer

def swap_front_back(self):

tmp=self.front;

tmpnxt=tmp.next;

while(tmp.next!=self.back):

if(tmp.next==self.back)

tmp.next=tmp;

(self.back).next=tmpnxt;