From the course: Python Data Structures: Linked Lists

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Inserting Nodes

Inserting Nodes

- [Instructor] So we've appended nodes to the end of our linked list. But how do we insert a node in the middle or even the beginning? And this is another case where linked lists generally outperform traditional lists. In a traditional list, you have to shuffle all the subsequent elements around to make room for a new one. In a linked list, you just write the new element to memory anywhere you want, and update a single link to point to it. Of course, there is one catch, there's always a catch, we do have to traverse to the point in the list where we want to insert it, we can't just hop to a random point in memory, but where do you want to insert it? Well, there are a few possibilities, but I want to introduce you to the concept of a sorted linked list. So we're going to make a linked list where every item must be in numerical ascending order. And this means that our insertion function needs to look for the right spot to insert the node, so it will go along until the next node has a…

Contents