Skip to content

Commit 92839b0

Browse files
Merge pull request #29 from yuvi-mittal/master
Doubly linked list
2 parents 44b19f1 + 6aae2dc commit 92839b0

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed

doublyLinkedList/DLL_question.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//Given a doubly linked list. Your task is to reverse the doubly linked list and return its head.
2+
3+
//solution code
4+
5+
class DLLNode {
6+
int data;
7+
DLLNode next;
8+
DLLNode prev;
9+
10+
DLLNode(int val) {
11+
data = val;
12+
next = null;
13+
prev = null;
14+
}
15+
}
16+
17+
class Solution {
18+
// Function to reverse a doubly linked list
19+
public DLLNode reverseDLL(DLLNode head) {
20+
if (head == null) {
21+
return null; // If the list is empty, return null
22+
}
23+
24+
DLLNode curr = head;
25+
DLLNode temp = null;
26+
27+
// Traverse the list and swap the next and prev pointers
28+
while (curr != null) {
29+
// Swap the next and prev pointers
30+
temp = curr.prev;
31+
curr.prev = curr.next;
32+
curr.next = temp;
33+
34+
// Move to the next node (which is prev now due to the swap)
35+
curr = curr.prev;
36+
}
37+
38+
// After the loop, temp will be pointing to the new head of the reversed list
39+
if (temp != null) {
40+
head = temp.prev; // Set the new head
41+
}
42+
43+
return head; // Return the new head of the reversed list
44+
}
45+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Node class representing each element of the doubly linked list
2+
class Node {
3+
int data;
4+
Node prev, next;
5+
6+
// Constructor to create a new node with a given data
7+
public Node(int data) {
8+
this.data = data;
9+
prev = next = null;
10+
}
11+
}
12+
13+
// DoublyLinkedList class for basic operations
14+
public class DoublyLinkedList {
15+
Node head;
16+
17+
// Constructor to initialize an empty doubly linked list
18+
public DoublyLinkedList() {
19+
head = null;
20+
}
21+
22+
// Insert a new node at the end of the doubly linked list
23+
public void insert(int data) {
24+
Node newNode = new Node(data);
25+
26+
// If the list is empty, make the new node the head
27+
if (head == null) {
28+
head = newNode;
29+
return;
30+
}
31+
32+
// Otherwise, traverse to the end of the list
33+
Node last = head;
34+
while (last.next != null) {
35+
last = last.next;
36+
}
37+
38+
// Update the next and prev pointers to insert the new node
39+
last.next = newNode;
40+
newNode.prev = last;
41+
}
42+
43+
// Delete a node with the given data from the doubly linked list
44+
public void delete(int data) {
45+
if (head == null) return; // List is empty, nothing to delete
46+
47+
Node current = head;
48+
49+
// Traverse the list to find the node to be deleted
50+
while (current != null && current.data != data) {
51+
current = current.next;
52+
}
53+
54+
// Node not found
55+
if (current == null) return;
56+
57+
// If the node to be deleted is the head
58+
if (current == head) {
59+
head = current.next;
60+
}
61+
62+
// Update pointers to remove the node
63+
if (current.next != null) {
64+
current.next.prev = current.prev;
65+
}
66+
67+
if (current.prev != null) {
68+
current.prev.next = current.next;
69+
}
70+
}
71+
72+
// Traverse the list from the head to the end
73+
public void traverseForward() {
74+
Node current = head;
75+
System.out.print("Doubly Linked List (forward): ");
76+
while (current != null) {
77+
System.out.print(current.data + " ");
78+
current = current.next;
79+
}
80+
System.out.println();
81+
}
82+
83+
// Traverse the list from the end to the head (reverse)
84+
public void traverseBackward() {
85+
if (head == null) return; // Empty list
86+
87+
// Traverse to the last node
88+
Node last = head;
89+
while (last.next != null) {
90+
last = last.next;
91+
}
92+
93+
// Traverse backward using the prev pointers
94+
System.out.print("Doubly Linked List (backward): ");
95+
while (last != null) {
96+
System.out.print(last.data + " ");
97+
last = last.prev;
98+
}
99+
System.out.println();
100+
}
101+
102+
// Search for a node with the given data
103+
public boolean search(int data) {
104+
Node current = head;
105+
106+
// Traverse the list to find the data
107+
while (current != null) {
108+
if (current.data == data) return true;
109+
current = current.next;
110+
}
111+
112+
return false;
113+
}
114+
115+
// Driver method for testing the doubly linked list implementation
116+
public static void main(String[] args) {
117+
DoublyLinkedList dll = new DoublyLinkedList();
118+
119+
// Inserting nodes into the doubly linked list
120+
dll.insert(10);
121+
dll.insert(20);
122+
dll.insert(30);
123+
dll.insert(40);
124+
dll.insert(50);
125+
126+
// Traversing forward
127+
System.out.println("Traversing forward:");
128+
dll.traverseForward(); // Expected output: 10 20 30 40 50
129+
130+
// Traversing backward
131+
System.out.println("\nTraversing backward:");
132+
dll.traverseBackward(); // Expected output: 50 40 30 20 10
133+
134+
// Deleting a node
135+
System.out.println("\nDeleting node 30:");
136+
dll.delete(30);
137+
dll.traverseForward(); // Expected output (without 30): 10 20 40 50
138+
139+
// Searching for elements
140+
System.out.println("\nSearching for 40: " + dll.search(40)); // Output: true
141+
System.out.println("Searching for 60: " + dll.search(60)); // Output: false
142+
}
143+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Doubly Linked List Documentation
2+
A Doubly Linked List (DLL) is a linear data structure where each node contains three fields: data,
3+
a reference to the next node, and a reference to the previous node. Unlike a singly linked list,
4+
a DLL allows traversal in both directions—forward and backward.
5+
6+
Two Components:
7+
8+
Node:
9+
1. Contains data.
10+
2. Contains a reference to the next node (next) and the previous node (prev).
11+
12+
Head Node:
13+
1. The first node in the list.
14+
2. If the list is empty, the head is null.
15+
16+
Operations:
17+
1. Insertion:
18+
New nodes can be inserted at the end of the list.
19+
Traversal happens from the head node to the end, where the new node is appended.
20+
21+
2. Deletion:
22+
Deletes a node with a specific value.
23+
Traverses the list to find the node, and updates the next and prev pointers to remove the node.
24+
25+
3. Traversal:
26+
Forward Traversal: Starts from the head and moves toward the end using the next pointer.
27+
Backward Traversal: Starts from the last node and moves backward using the prev pointer.
28+
29+
4. Search:
30+
Searches for a node with a given data value.
31+
Traverses the list to find the node and returns true if found, otherwise false.
32+
33+
34+
Time Complexity:
35+
36+
Insertion:
37+
O(n) in the worst case, as you may need to traverse the entire list to insert at the end.
38+
39+
Deletion:
40+
O(n) to locate the node to delete (as traversal is required).
41+
42+
Traversal:
43+
O(n), where n is the number of nodes in the list.
44+
45+
Search:
46+
O(n), since you may need to traverse the entire list to find a specific node.

0 commit comments

Comments
 (0)