A Doubly linked list is a linked list that consists of a set of sequentially linked records called nodes. Each node contains two fields that are references to the previous and to the next node in the sequence of nodes.
The task is to create a doubly linked list by inserting nodes such that list remains in ascending order on printing from left to right. Also, we need to maintain two pointers, head (points to first node) and tail (points to last node).
Input : 40 50 10 45 90 100 95 Output :10 40 45 50 90 95 100 Input : 30 10 50 43 56 12 Output :10 12 30 43 50 56
The task can be accomplished as:
- If Linked list is empty then make both the left and right pointers point to the node to be inserted and make its previous and next field point to NULL.
- If node to be inserted has value less than the value of first node of linked list then connect that node from previous field of first node.
- If node to be inserted has value more than the value of last node of linked list then connect that node from next field of last node.
- If node to be inserted has value in between the value of first and last node, then check for appropriate position and make connections.
Doubly linked list on printing from left to right 10 23 30 40 50 60 90 95 110
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Insert value in sorted way in a sorted doubly linked list
- Merge K sorted Doubly Linked List in Sorted Order
- Given a linked list which is sorted, how will you insert in sorted way
- Sorted merge of two sorted doubly circular linked lists
- Difference between Singly linked list and Doubly linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x
- Remove duplicates from a sorted doubly linked list
- Sort a k sorted doubly linked list
- Find pairs with given product in a sorted Doubly Linked List
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- How to write C functions that modify head pointer of a Linked List?
- Find kth node from Middle towards Head of a Linked List
- Make middle node head in a linked list
- Delete a Node from linked list without head pointer
- Sorted insert for circular linked list
- Construct a Doubly linked linked list from 2D Matrix
- Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
- Pairwise swap adjacent nodes of a linked list by changing pointers | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.