Can Recursion Reverse a Singly Linked List in Descending Order?

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Elements
In summary, the conversation revolves around discussing a recursive algorithm for creating a new list in ascending order from a given singly linked list in descending order. The algorithm involves adding elements to the new list while going down the recursion levels and can be done in two ways - by keeping track of the tail or by adding elements at the head of the new list. The complexity of the algorithm is determined to be $T(n)=T(n-1)+c$. The use of pointers in the algorithm does not make a difference in its time complexity.
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
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)
 
  • #3
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}$$
 
  • #4
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)
 
  • #5
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)
 
  • #6
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.
 
  • #7
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)
 
  • #8
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)
 
  • #9
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)
 

1. What is the definition of "Elements in ascending order"?

Elements in ascending order refer to the arrangement of elements on the periodic table in order of increasing atomic number.

2. Why are elements arranged in ascending order on the periodic table?

Elements are arranged in ascending order on the periodic table to show the periodic pattern of properties and behaviors of elements based on their atomic structure.

3. How many elements are there in ascending order on the periodic table?

As of 2021, there are 118 elements in ascending order on the periodic table, with the most recently discovered element being oganesson (Og).

4. What is the significance of the position of an element in ascending order on the periodic table?

The position of an element in ascending order on the periodic table determines its properties, such as its atomic mass, electron configuration, and chemical reactivity.

5. Is there a specific group or pattern for elements in ascending order on the periodic table?

Yes, elements in ascending order on the periodic table are grouped into periods (horizontal rows) and groups (vertical columns) based on their similar properties and behaviors.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
29
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
20
Views
4K
  • Programming and Computer Science
Replies
7
Views
2K
Back
Top