Skip to main content

Command Palette

Search for a command to run...

Linked List (3) - Prepend, Pop First, Get, Set and Insert

Updated
3 min read
K

I'm currently learning Python and studying RAG (Retrieval-Augmented Generation).

I haven't taken the course because I had to work on my paper…

Today, I learned about Prepend, Pop First, Get, Set, and Insert in Linked List(LL).

Prepend

 def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True

The prepend method adds a new node to the front of the list, before the current head node.
An important detail when implementing this is to keep the length of the Linked List updated.

Pop First

 def pop_first(self):
        if self.length == 0:
            return None
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp

The pop_first method removes the first node (head) of the list and returns it.

If the list is empty (self.length == 0), it returns None.

When a node is removed, it decreases the length by one.

If the list becomes empty after the operation, it also updates self.tail to None.

Get

def get(self, index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _ in range(index):
            temp = temp.next
        return temp

The get method retrieves the node at the given index.

If the index is out of bounds (less than 0 or greater than or equal to self.length), it returns None.

Otherwise, it traverses the list from the head to the desired position and returns the node.

Set

def set_value(self, index, value):
        temp = self.get(index)
        if temp:
            temp.value = value
            return True
        return False

The set_value method updates the value of the node at a given index with the new value.

It uses the get method to find the node; if the node exists, it assigns the new value and returns True.

If no node is found at the index, it returns False.

Insert

def insert(self, index, value):
        if index < 0 or index > self.length: 
        # This part differs from the 'get' method, 
        # as index > self.length is allowed here to enable adding at the end.
            return False
        if index == 0:
        # Simply calls the 'prepend' method, reusing existing functionality.
            return self.prepend(value) 

        if index == self.length:
         # Adding to the end of the list, for example, 
         # adding at index 4 in a list with a length of 4.
            return self.append(value)
        new_node = Node(value)
        temp = self.get(index - 1)
        new_node.next = temp.next
        temp.next = new_node
        self.length += 1
        return True

The insert method adds a new node at a specified index.

If the index is invalid (less than 0 or greater than self.length), it returns False.

It handles specific cases: if index is 0, it prepends, and if index is equal to self.length, it appends.

For other indices, it inserts the new node between existing nodes, adjusting pointers and updating the length.

Python Algorithm Study Journal

Part 5 of 7

This series is a personal study journal documenting my journey learning Python algorithms for coding tests. I'll share challenges, solutions, and insights as I work through different algorithm problems, hoping to learn and grow step by step.

Up next

Linked List (2) - Append and Pop

Append def append(self, value): new_node = Node(value) if self.head is None: self.head = new_node self.tail = new_node else: self.tail.next = new_node self.tail = new_node self.length += 1 Explanation ...

More from this blog

The KiwiChip's Code Nest

8 posts