A doubly linked list is a data structure where a set of sequential links of records called nodes exist. Unlike the singly linked list, a node of a doubly linked list consists of three fields: two link fields and one information field. Two link fields provide information about the address of previous nodes and the next nodes in the sequence and one data field.
The link fields are also known as “previous” and “next” pointers and store the addresses of the previous and next nodes in the list. And the previous pointer of the very first node, as well as the next pointer of the last node points to a Null value or called a sentinel node.
Syntax:
Struct node{ int data; struct node *next, *prev; *head;
Where struct keyword refers to a structure, node is the name of the structure, data is the information field which contains only integer values, *next is the pointer of type structure which holds the address of the next node in the sequential list, and *prev is the pointer of the type structure and hold the address of the previous node in the sequential list.
Insert at the first position: When a new node is inserted at the first position of the doubly linked list, it becomes the new head. Start by creating the node and inserting the data value in it. Next change the pointer references of the old and new head node. The prev pointer of the new node should be pointed to a NULL value, whereas the next pointer should be pointed towards the old head. Similarly, the prev pointers of the old head which was set to NULL should be pointing to the new head.
Insert at the end: When a new node is inserted at the last position of the doubly linked list, it requires a couple of changes as the previous last node now becomes the second last node. The next pointer of the previous last node should now be changed from null value and point towards the newly inserted node. The next of the newly inserted last node should now point towards null value and the prev pointer of this very node points to the older last node which is now the second last node.
Insert before: Suppose we have a node called A and the next node to A is C. Now we have to insert a node before C. When a new node is created which is to be placed before C requires the following changes. First the next pointer of this new node will point to node C and the prev pointer of this new node will point to A. Whereas the next pointer of node A will point to the newly inserted node and the previous pointer of node C will now point to the node inserted before C.
Insert after: Suppose we have a node called A and the next note to A is C. Now to insert a node after node we follow the steps similar to when we insert a node before any particular node. If a node B is to be placed after node A then the following changes need to be encountered. First we start with creating a new node and this node needs to be inserted after node A so the previous pointer of this node B will point to node A and the next of this node will point to node C. Now the next pointer of node A will point to node B, and the previous of node C will point to node B.
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Sort Binary Linked List | 200 |
|
33:21 | |
Partition List | 275 |
|
49:36 | |
Insertion Sort List | 300 |
|
49:25 | |
Sort List | 350 |
|
59:53 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Palindrome List | 200 |
|
47:06 | |
Remove Duplicates from Sorted List II | 300 |
|
57:51 | |
Merge Two Sorted Lists | 300 |
|
29:11 | |
Remove Duplicates from Sorted List | 300 |
|
17:58 | |
Remove Nth Node from List End | 350 |
|
27:38 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
K reverse linked list | 200 |
|
60:30 | |
Even Reverse | 200 |
|
45:58 | |
Swap List Nodes in pairs | 350 |
|
33:34 | |
Rotate List | 350 |
|
33:58 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Kth Node From Middle | 200 |
|
30:12 | |
Reverse Alternate K Nodes | 300 |
|
54:12 | |
Reverse Link List II | 450 |
|
57:24 | |
Reorder List | 600 |
|
57:10 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Add Two Numbers as Lists | 250 |
|
43:07 | |
List Cycle | 600 |
|
39:15 |