edited by
440 views
1 1 vote

 

Consider the following code fragment.

class Node:
    def __init__(self, data, next=None):
        self.data = data
        self.next = next

def invert_list(front):
    curr = front
    prev = None
    next = curr.next
    while curr is not None : 
	(*)
    front = prev
    return front

 

Which code must be added in the part marked (*) so the above code correctly inverts a non-empty singly linked list? See the figure to understand what ”invert” means.
 

  1. next.next = prev; prev = curr; curr = next; 
    if next is not None: next = next.next

     

  2. curr.next = prev; prev = curr; curr = next_node; 
    if next is not None: next = next.next

     

  3. next.next = curr; prev = curr; curr = next; 
    if next is not None: next = next.next

     

  4. prev = curr; curr = next; curr.next = prev; 
    if next is not None: next = next.next

1 Answer

3 3 votes
$ I\, think \, that \, is \, printing \, mistake.$
$ if\, treated\, as \, next\, that\, is\, yeilding\, the \, Desired \, results.$
Answer:
Position:
Show:

Related questions

3 3 votes
1 1 answer
359
359 views
GO Classes asked Sep 15, 2024
359 views
class Node: def __init__(self, data, next=None): self.data = data self.next = next def print_nodes(ptr): if ptr: print(ptr.data, end=' ') while ptr.next: ptr = ptr.next p...
3 3 votes
1 1 answer
592
592 views
GO Classes asked Sep 15, 2024
592 views
The following code is intended to remove a node p from a doubly linked list. Assume that we know that p is in the list, so the list is not empty.class Node: def __init__(...
4 4 votes
2 2 answers
433
433 views
GO Classes asked Sep 15, 2024
433 views
Consider a mutual pair of recursive functions g() and h().class Node: def __init__(self, value, next=None): self.value = value self.next = next def g(l): if l is None or ...
0 0 votes
2 2 answers
367
367 views
GO Classes asked Sep 15, 2024
367 views
Consider the following function that takes reference to head of a Doubly Linked List as parameter. Assume that a node of doubly linked list has previous pointer as $\text...