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
SUMMARY

The discussion focuses on implementing a recursive algorithm to reverse a singly linked list sorted in descending order into ascending order. The proposed function, Node *Ch(Node *L1), utilizes recursion to traverse the list and create a new list L2. Participants confirm that the algorithm correctly constructs L2 by adding elements at the head or tail, and they discuss the time complexity, concluding that it remains T(n)=T(n-1)+c despite the use of pointers.

PREREQUISITES
  • Understanding of singly linked lists and their structure
  • Familiarity with recursion and recursive algorithms
  • Basic knowledge of C++ programming, particularly pointers and node manipulation
  • Concept of time complexity analysis in algorithms
NEXT STEPS
  • Implement the recursive algorithm for reversing a singly linked list in C++
  • Explore different methods of adding nodes to a linked list (head vs. tail insertion)
  • Study the implications of pointer usage on algorithm complexity
  • Learn about iterative approaches to reversing linked lists for comparison
USEFUL FOR

Software developers, computer science students, and anyone interested in data structures and algorithms, particularly those focusing on linked list manipulations.

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