Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Computer Science

Insert into Sorted Linked List Pseudocode

Does the following pseudocode accomplish inserting into a sorted linked list?

  • Input: a number a and a sorted linked list L of numbers
  • Output: a sorted list including a and L.
insertInSortedList(L,a)
1:      x = L.head
2:      while x ≠ NIL
3:      x = x.next
4:      if x.prev < a and x.next > a
1:      // Insert a into the linked list        
4:          x.next.prev = a
5:      x.next = a

1 Answer

Steven Parker
Steven Parker
231,184 Points

It's not clear what the non-sequential numbers on the left side mean, and the lack of additional indentation makes it unclear which line(s) are part of the "while" loop.

Also, what if the value being added preceeds the very first (head) item, or should follow the very last (tail) item?