Author Topic: linked list sort  (Read 593 times)

  • Full Member
  • ***
  • Posts: 114
  • Karma: +6/-3
    • View Profile
linked list sort
« on: February 06, 2013, 12:08:25 pm »
Give most efficient solution O(n) time complexity O(1) space:

Given a linked list of 0s, 1s and 2s, sort it.

Share on Bluesky Share on Facebook


  • Moderator
  • Full Member
  • *****
  • Posts: 126
  • Karma: +1/-0
  • Location: Puttaparthi
    • View Profile
Re: linked list sort
« Reply #1 on: February 06, 2013, 12:35:12 pm »
Declare three counters temp0, temp1, temp2;

Now parse through the list and update these counters;

After the first parsing we have number of 0s,1s and 2s in the list.

Now from starting with head node of the list fill it first with 0s and then 1s and then 2s

I hope it is correct
Don't think you are. Know you are -Kranthi

  • Full Member
  • ***
  • Posts: 114
  • Karma: +6/-3
    • View Profile
Re: linked list sort
« Reply #2 on: February 06, 2013, 01:27:37 pm »
yes it is correct

Can you think of another method?