We also often sort data structures in order to organize the values stored in them. In this exercise, you will sort a linked list from smallest value to largest value.

To sort a linked list, we can do the following:

  1. Instantiate a new linked list
  2. Find the maximum value of our inputted linked list
  3. Insert the maximum to the beginning of the new linked list
  4. Remove the maximum value from the inputted linked list
  5. Repeat steps 2-4 until the head node of the inputted linked list points to None
  6. Return the new linked list



Fill in the sort_linked_list function such that you return a new linked list that is sorted from smallest value to largest value. Step 1 and Step 6 have been completed for you.

Use the methods in linkedlist.py and node.py to traverse, remove, and get values from the list.

Test cases have been provided for you in order to test your code.


Using the steps described in the narrative to write your sort function, what would be the big O runtime of the function? We will learn how to sort data structures with faster runtimes in future courses.

Scroll to the bottom of the code and change the value of runtime to whichever one of these values you think the big O runtime of sort_linked_list is:

  • "1"
  • "N"
  • "log N"
  • "N log N"
  • "N^2"
  • "2^N"
  • "N!"

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?