Skip to content

Doubly linked list #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions doublyLinkedList/DLL_question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//Given a doubly linked list. Your task is to reverse the doubly linked list and return its head.

//solution code

class DLLNode {
int data;
DLLNode next;
DLLNode prev;

DLLNode(int val) {
data = val;
next = null;
prev = null;
}
}

class Solution {
// Function to reverse a doubly linked list
public DLLNode reverseDLL(DLLNode head) {
if (head == null) {
return null; // If the list is empty, return null
}

DLLNode curr = head;
DLLNode temp = null;

// Traverse the list and swap the next and prev pointers
while (curr != null) {
// Swap the next and prev pointers
temp = curr.prev;
curr.prev = curr.next;
curr.next = temp;

// Move to the next node (which is prev now due to the swap)
curr = curr.prev;
}

// After the loop, temp will be pointing to the new head of the reversed list
if (temp != null) {
head = temp.prev; // Set the new head
}

return head; // Return the new head of the reversed list
}
}
143 changes: 143 additions & 0 deletions doublyLinkedList/doublyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Node class representing each element of the doubly linked list
class Node {
int data;
Node prev, next;

// Constructor to create a new node with a given data
public Node(int data) {
this.data = data;
prev = next = null;
}
}

// DoublyLinkedList class for basic operations
public class DoublyLinkedList {
Node head;

// Constructor to initialize an empty doubly linked list
public DoublyLinkedList() {
head = null;
}

// Insert a new node at the end of the doubly linked list
public void insert(int data) {
Node newNode = new Node(data);

// If the list is empty, make the new node the head
if (head == null) {
head = newNode;
return;
}

// Otherwise, traverse to the end of the list
Node last = head;
while (last.next != null) {
last = last.next;
}

// Update the next and prev pointers to insert the new node
last.next = newNode;
newNode.prev = last;
}

// Delete a node with the given data from the doubly linked list
public void delete(int data) {
if (head == null) return; // List is empty, nothing to delete

Node current = head;

// Traverse the list to find the node to be deleted
while (current != null && current.data != data) {
current = current.next;
}

// Node not found
if (current == null) return;

// If the node to be deleted is the head
if (current == head) {
head = current.next;
}

// Update pointers to remove the node
if (current.next != null) {
current.next.prev = current.prev;
}

if (current.prev != null) {
current.prev.next = current.next;
}
}

// Traverse the list from the head to the end
public void traverseForward() {
Node current = head;
System.out.print("Doubly Linked List (forward): ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

// Traverse the list from the end to the head (reverse)
public void traverseBackward() {
if (head == null) return; // Empty list

// Traverse to the last node
Node last = head;
while (last.next != null) {
last = last.next;
}

// Traverse backward using the prev pointers
System.out.print("Doubly Linked List (backward): ");
while (last != null) {
System.out.print(last.data + " ");
last = last.prev;
}
System.out.println();
}

// Search for a node with the given data
public boolean search(int data) {
Node current = head;

// Traverse the list to find the data
while (current != null) {
if (current.data == data) return true;
current = current.next;
}

return false;
}

// Driver method for testing the doubly linked list implementation
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();

// Inserting nodes into the doubly linked list
dll.insert(10);
dll.insert(20);
dll.insert(30);
dll.insert(40);
dll.insert(50);

// Traversing forward
System.out.println("Traversing forward:");
dll.traverseForward(); // Expected output: 10 20 30 40 50

// Traversing backward
System.out.println("\nTraversing backward:");
dll.traverseBackward(); // Expected output: 50 40 30 20 10

// Deleting a node
System.out.println("\nDeleting node 30:");
dll.delete(30);
dll.traverseForward(); // Expected output (without 30): 10 20 40 50

// Searching for elements
System.out.println("\nSearching for 40: " + dll.search(40)); // Output: true
System.out.println("Searching for 60: " + dll.search(60)); // Output: false
}
}
46 changes: 46 additions & 0 deletions doublyLinkedList/doublyLinkedList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Doubly Linked List Documentation
A Doubly Linked List (DLL) is a linear data structure where each node contains three fields: data,
a reference to the next node, and a reference to the previous node. Unlike a singly linked list,
a DLL allows traversal in both directions—forward and backward.

Two Components:

Node:
1. Contains data.
2. Contains a reference to the next node (next) and the previous node (prev).

Head Node:
1. The first node in the list.
2. If the list is empty, the head is null.

Operations:
1. Insertion:
New nodes can be inserted at the end of the list.
Traversal happens from the head node to the end, where the new node is appended.

2. Deletion:
Deletes a node with a specific value.
Traverses the list to find the node, and updates the next and prev pointers to remove the node.

3. Traversal:
Forward Traversal: Starts from the head and moves toward the end using the next pointer.
Backward Traversal: Starts from the last node and moves backward using the prev pointer.

4. Search:
Searches for a node with a given data value.
Traverses the list to find the node and returns true if found, otherwise false.


Time Complexity:

Insertion:
O(n) in the worst case, as you may need to traverse the entire list to insert at the end.

Deletion:
O(n) to locate the node to delete (as traversal is required).

Traversal:
O(n), where n is the number of nodes in the list.

Search:
O(n), since you may need to traverse the entire list to find a specific node.
Loading