Move Last Node to Front of Linear Linked List

Python - Move Last Node to Front of Linear Linked List

What’s up everyone?!  I hope everyone’s been doing well as always. Today I am going to work on another linear linked list problem.

The prompt is: Write a function to move the last node to the front of a linear linked list. You could do this iteratively or recursively, I’m just going to go with iteratively for this one.

    # move last to front
    def moveLastToFront(self):

        current = self.head
        previous = None
        temp = None

        # empty or single node
        if not current or not current.next:
            return

        # traverse the list
        while current is not None and current.next is not None:
            temp = current
            previous = current
            current = current.next

        previous.next = None

        # you could write your own push function here
        self.addBeginning(temp.data)

Of course there are much more efficient ways of writing this. I would say the trick is how to keep track of the last node and the node before last, that way you do not lose the data that you’re trying to move. If you have questions, feel free to post in the comments below!

Linear Linked List – Search Element using Python

Linear Linked List using Python

What’s up friends?!?

Let’s continue on another linear linked list problem. I’m going to make this post short since I’ll use the boilerplate code from the last LLL problem we had. If you need any help, I’ll also post the link to GitHub so you can stroll along.

Anyways, for this problem, I just want to write some code to find a specific element inside the LLL. You could do this iteratively or recursively, whichever way you choose to do, but for the purpose of this program, I am just going to solve it iteratively.

Continue reading “Linear Linked List – Search Element using Python”

Linear Linked List – Insert Element Beginning using Python

Linear Linked List - Insert Element at Beginning using Python

What’s up friends?! Hope your week has been great as always, the weather here has been quite chilly and then hot at different days, so I’ve been taking advantage of it and go hiking quite a bit to clear up my head.

Today, I want to take a moment to introduce data structures. Data structures is one of my favorite topics ever, and in my personal opinion, possibly one of the most important courses of computer science in general, as it provides the foundation for later on. Well, WHAT IS DATA STRUCTURE? Data structure essentially is a way of organizing data for the machine to work efficiently, and you have seen it before, such as array! As such, I want to talk about linear linked list because I am positive colleges would definitely throw these at you, and you definitely are going to spend hours and hours on data structures!

Continue reading “Linear Linked List – Insert Element Beginning using Python”