Prepin
Log in

Create a doubly linked list.

Approach / Explanation

This question requires you to create a particular type of data structure known as a doubly linked list. Unlike a singly linked list, a doubly linked list features nodes that connect to both the next and the previous nodes. This enables traversal in both directions, whereas a singly linked list allows movement in only one direction. You will likely need to define a class or struct for the list's nodes, which should include at least two properties: one for the data stored and another for the pointers to the next and previous nodes. Additionally, you will need to implement methods to add, remove, and possibly locate nodes within your list. Keep in mind that the fundamental design of a doubly linked list must allow for traversal in both backward and forward directions. Key points include: Understand the concept of a doubly linked list and how it contrasts with a singly linked list. Create the structure for the nodes. Establish the structure for the list. Develop methods for adding, removing, and possibly locating items in the list.

Suggested Answer

A straightforward implementation in Python might appear as follows: class Node : def __init__ ( self , data = None ) : self . data = data self . next = None self . prev = None class LinkedList : def __init__ ( self ) : self . head = None def add ( self , data ) : node = Node ( data ) if self . head is not None : self . head . prev = node node . next = self . head self . head = node def remove ( self , data ) : current = self . head while current is not None : if current . data == data : if current . prev is not None : current . prev . next = current . next else : self . head = current . next if current . next is not None : current . next . prev = current . prev else : current . prev . next = None return True current = current . next return False This example features two classes: the Node class, which signifies each individual node, and the LinkedList class, which represents the entire list. The add method enables us to insert a node into the list, while the remove method facilitates the removal of a node from the list.

Alternative Answer

This is the same concept implemented in Java: class Node { int data ; Node next , prev ; Node ( int data ) { this . data = data ; next = prev = null ; } } class DoublyLinkedList { Node head ; public void add ( int data ) { Node newNode = new Node ( data ) ; newNode . next = head ; newNode . prev = null ; if ( head != null ) head . prev = newNode ; head = newNode ; } public void remove ( Node node ) { if ( head == node ) head = node . next ; if ( node . next != null ) node . next . prev = node . prev ; if ( node . prev != null ) node . prev . next = node . next ; } } In this Java implementation, the Node class represents a single node, while the DoublyLinkedList class manages the entire list. The methods for add and remove are included, akin to the Python example. Additionally, null checks are implemented before setting the next and previous pointers to prevent potential NullPointerExceptions.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Apple

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.