Can Recursion Reverse a Singly Linked List in Descending Order?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Elements
Click For Summary

Discussion Overview

The discussion revolves around creating a recursive algorithm to reverse a singly linked list that is initially in descending order, transforming it into ascending order. Participants explore the implementation details and the implications of using recursion in this context.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Some participants propose a recursive function that traverses the list and constructs a new list in ascending order, but the implementation details are debated.
  • There is a suggestion that the new list can be built by adding elements to the head of the new list during the recursive descent, eliminating the need for a tail pointer.
  • Participants discuss the expected output of the algorithm when applied to a specific example, with some asserting that the output should be the reversed list.
  • Concerns are raised about the complexity of the algorithm, with questions regarding whether the presence of pointers affects the time complexity.
  • One participant suggests that the time complexity can be expressed as $T(n)=T(n-1)+c$, indicating a recursive relationship.

Areas of Agreement / Disagreement

Participants express differing views on the implementation details of the recursive function and the best approach to constructing the new list. There is no consensus on the optimal method, and the discussion remains unresolved regarding the specific implementation and its implications.

Contextual Notes

Participants note that the algorithm's complexity may not be affected by the use of pointers, but this remains a point of discussion. The exact implementation details and assumptions about the linked list structure are not fully clarified.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

I have to write a recursive algorithm, that, given a list in descending order with $M$ elements, creates a new list, that contains the element of the list in ascending order. The list is singly linked and we are given a pointer that shows to the first element of the list.

That's what I have tried:

Code:
Node *Ch(Node *L1){ 
      if (L1.next!= NULL) 
          Ch(L1.next) 
      // Create a new node with value L1.data and the next field is NULL
      // then we add this node to the List L2
      return L2; 
}

Could you tell me if it is right? (Thinking)
 
Technology news on Phys.org
evinda said:
Hello! (Wave)

I have to write a recursive algorithm, that, given a list in descending order with $M$ elements, creates a new list, that contains the element of the list in ascending order. The list is singly linked and we are given a pointer that shows to the first element of the list.

That's what I have tried:

Code:
Node *Ch(Node *L1){ 
      if (L1.next!= NULL) 
          Ch(L1.next) 
      // Create a new node with value L1.data and the next field is NULL
      // then we add this node to the List L2
      return L2; 
}

Could you tell me if it is right? (Thinking)

Hi! (Smile)

Suppose we apply this to:
$$\boxed{3}\to\boxed{2}\to\boxed{1}\to \text{NULL}$$

What will we get? (Wondering)
 
I like Serena said:
Hi! (Smile)

Suppose we apply this to:
$$\boxed{3}\to\boxed{2}\to\boxed{1}\to \text{NULL}$$

What will we get? (Wondering)

Don't we get this? (Thinking)

$$\boxed{1}\to\boxed{2}\to\boxed{3}\to \text{NULL}$$
 
evinda said:
Don't we get this? (Thinking)

$$\boxed{1}\to\boxed{2}\to\boxed{3}\to \text{NULL}$$

First you go down 3 recursion levels.

Then you add $1$ to L2, so we get: $L2\to \boxed{1} \to \text{NULL}$.

Then we back up a recursion level and add $2$ to L2 to get: $L2\to \boxed{2} \to \boxed{1} \to \text{NULL}$.

And finally add $3$: $L2\to \boxed{3} \to \boxed{2} \to \boxed{1} \to \text{NULL}$.

Or isn't that what you had in mind? (Wondering)
 
I like Serena said:
First you go down 3 recursion levels.

Then you add $1$ to L2, so we get: $L2\to \boxed{1} \to \text{NULL}$.

Then we back up a recursion level and add $2$ to L2 to get: $L2\to \boxed{2} \to \boxed{1} \to \text{NULL}$.

And finally add $3$: $L2\to \boxed{3} \to \boxed{2} \to \boxed{1} \to \text{NULL}$.

Or isn't that what you had in mind? (Wondering)

With these:
// Create a new node with value L1.data and the next field is NULL
// then we add this node to the List L2

I mean that at L2, we would have a pointer R that points to the last element and at R.next we would put the new element.

Is it wrong? (Thinking)
 
evinda said:
With these:
// Create a new node with value L1.data and the next field is NULL
// then we add this node to the List L2

I mean that at L2, we would have a pointer R that points to the last element and at R.next we would put the new element.

Is it wrong? (Thinking)

Then it's okay.
But I guess you should write it out more explicitly. (Nerd)

And alternatively you could add elements to L2 while going down.
Then you can add them to the head of L2 and there is no need to keep track of a tail.
 
I like Serena said:
Then it's okay.
But I guess you should write it out more explicitly. (Nerd)

A ok! (Nod)

I like Serena said:
And alternatively you could add elements to L2 while going down.
Then you can add them to the head of L2 and there is no need to keep track of a tail.

How could we do it like that? (Thinking)
 
Also how can we find the complexity of the algorithm? (Thinking)

Is it $T(n)=T(n-1)+c$? Or is there a difference, now that we have pointers? (Worried)
 
evinda said:
How could we do it like that? (Thinking)

For instance like:
Code:
Node *Ch(Node *L1){ 
      if (L1!= NULL)
          next = L1.next
          Move the node to which L1 points to the head of L2
          Ch(next) 
      return L2; 
}
(Thinking)

evinda said:
Also how can we find the complexity of the algorithm? (Thinking)

Is it $T(n)=T(n-1)+c$? Or is there a difference, now that we have pointers? (Worried)

Yes it is.
The pointers do not make a difference. (Nod)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
6
Views
2K
Replies
9
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K