1

Scenario is as follows:-

I want to reverse the direction of the singly linked list, In other words, after the reversal all pointers should now point backwards..

Well the algorithm should take linear time.

The solution that i have thought of using another datastructure A Stack.. With the help of which the singly linked list would be easily reversed, with all pointers pointing backwards.. But i am in doubt, that whether the following implementation yeild linear time complexity.. Please comment on this.. And if any other efficient algorithm is in place, then please discuss..

Thanks.

Peter
  • 127,331
  • 53
  • 180
  • 211
AGeek
  • 5,165
  • 16
  • 56
  • 72
  • 1
    is http://stackoverflow.com/questions/1801549/reverse-a-singly-linked-list what you want? – Aif May 22 '10 at 17:23
  • Are There any other efficient solution available apart from using stack implementation,, Also How can we implement the same using recursion.. – AGeek May 22 '10 at 17:28
  • 1
    I don't think recursion suits this problem very well – Carson Myers May 22 '10 at 17:29
  • Do you think recursion suits any problem very well? I can't think of a convincing reason why *not* to use recursion here. – Daniel Harms May 22 '10 at 18:24
  • 1
    @Precision: Imagine a huge list. You could overflow the stack if you used recursion. –  May 22 '10 at 19:00
  • Fair enough. Then a single pass in place algorithm would be the best fit. – Daniel Harms May 22 '10 at 19:03
  • A singly-linked list _is_ a stack when you only hold on to the head of the list! Use it as one, and you're practically done. (If you don't understand how a singly linked list is a stack, think about the two fundamental operations you can use in a stack, and the equivalent operations in a singly linked list.) – Rex Kerr May 22 '10 at 19:59

6 Answers6

4

You could do it like this: As long as there are nodes in the input list, remove its first node and insert it at the beginning of the output list:

node* reverse(node *in) {
   out = NULL;
   while (in) {
      node = in;
      in = in->next;
      node->next = out;
      out = node;
   }
   return out;
}
sth
  • 222,467
  • 53
  • 283
  • 367
2

If you put all of the nodes of your linked list in a stack, it will run in linear time, as you simply traverse the nodes on the stack backwards.

However, I don't think you need a stack. All you need to remember is the node you were just at, to reverse the pointer of the current node. Make note of the next node before you reverse the pointer at this node.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
2

2 times O(N) = O(2*n) is still O(N). So first push N elements and then popping N elements from a stack is indeed linear in time, as you expected.

See also the section Multiplication by a Constant on the "Big O Notation" wikipedia entry.

catchmeifyoutry
  • 7,179
  • 1
  • 29
  • 26
1

The previous answers have and already (and rightly) mentioned that the solution using pointer manipulation and the solution using stack are both O(n).

The remaining question is to compare the real run time (machine cycle complexity) performance of the two different implementations of the reverse() function.

I expect that the following two aspects might be relevant:

  1. The stack implementation. Does it require the maximum stack depth to be explicitly specified? If so, how is that specified? If not, how the stack does memory management as the size grows arbitrarily large?

  2. I guess that nodes have to be copied from list to stack. [Is there a way without copying?] In that case, the copy complexity of the node needs to be accounted for. Thats because the size of the node can be (arbitrarily) large.

Given these, in place reversal by manipulating pointers seems more attractive to me.

Arun
  • 19,750
  • 10
  • 51
  • 60
0

For a list of size n, you call n times push and n times pop, both of which are O(1) operations, so the whole operation is O(n).

Dimitris Andreou
  • 8,806
  • 2
  • 33
  • 36
0

You can use a stack to achieve a O(n) implementation. But the recursive solution IS using a stack (THE stack)! And, like all recursive algorithms, it is equivalent to looping. However, in this case, using recursion or an explicit stack would create a space complexity of O(n) which is completely unnecessary.

Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41