• edited by
389 views
0 0 votes

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 $\textit{prev}$ and next pointer as $\textit{next}$.

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

def fun(head_ref):
    temp = None
    current = head_ref
    while current is not None:
        temp = current.prev
        current.prev = current.next
        current.next = temp
        current = current.prev
    if temp is not None:
        head_ref = temp.prev

Assume that reference of head of following doubly linked list is passed to above function

$1 \leftrightarrow 2 \leftrightarrow 3 \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6$. What should be the modified linked list after the function call? 

  1. $2 \leftrightarrow 1 \leftrightarrow 4 \leftrightarrow 3 \leftrightarrow 6 \leftrightarrow 5$
  2. $5 \leftrightarrow 4 \leftrightarrow 3 \leftrightarrow 2 \leftrightarrow 1 \leftrightarrow 6$
  3. $6  \leftrightarrow 5 \leftrightarrow 4 \leftrightarrow 3 \leftrightarrow 2 \leftrightarrow 1$
  4. $6 \leftrightarrow 5 \leftrightarrow 4 \leftrightarrow 3 \leftrightarrow 1 \leftrightarrow 2$

2 Answers

0 0 votes

Step-by-Step Reversal

Let’s reverse the list step by step:

  1. Start with current = 1 (head of the list).

    • temp = None (initial value).
    • Swap prev and next for node 1:
      • prev becomes 2, and next becomes None.
    • Move current to 2 (new prev).
  2. At current = 2:

    • temp = 1 (previous node).
    • Swap prev and next for node 2:
      • prev becomes 3, and next becomes 1.
    • Move current to 3.
  3. At current = 3:

    • temp = 2 (previous node).
    • Swap prev and next for node 3:
      • prev becomes 4, and next becomes 2.
    • Move current to 4.
  4. Repeat the process for nodes 4, 5, and 6.

After the final iteration:

  • The pointers for all nodes are reversed.
  • temp holds the reference to node 6 (original tail, now the head of the reversed list).

 

Correct Answer

The modified doubly linked list after the function call is:
6 ↔ 5 ↔ 4 ↔ 3 ↔ 2 ↔ 1.

Answer:
Position:
Show:

Related questions

3 3 votes
1 1 answer
378
378 views
GO Classes asked Sep 15, 2024
378 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...
1 1 vote
1 1 answer
467
467 views
GO Classes asked Sep 15, 2024
467 views
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 n...
3 3 votes
1 1 answer
624
624 views
GO Classes asked Sep 15, 2024
624 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
457
457 views
GO Classes asked Sep 15, 2024
457 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 ...