DEV Community

tartope
tartope

Posted on

Doubly Linked List Series: Creating shift() and unshift() methods with JavaScript

Building off of last week's blog on Doubly Linked List classes, push(), and pop() methods - linked here - today I will be discussing shift() and unshift() methods. Starting with shift(), this method does not take a parameter and removes the first node from the beginning of the list. Here is an illustration of a shift() method:

Drawing of shift method

Edge case: you can account for an empty list, or has only one node. If neither of those cases applies, then follow the steps above.

Here is an example of code for a shift() method:

Screen shot of shift method code

The method unshift() adds a node to the beginning of the list. Here is an illustration of this method:

Drawing of unshift method

Edge case: you can account for an empty list. If the list is not empty, then follow the steps mentioned above.

Here is an example of code for an unshift() method:

Screen shot of unshift method code

The time complexity for shift() and unshift() methods for a Doubly Linked List is O(1) constant time. The length of the list does not impact the number of steps to remove from the beginning or add to the beginning of the list.

In the next part of this series, I will share what I have learned about creating get() and set() methods for a Doubly Linked List.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay