DS - 1
DS - 1
Singly linked list is the simplest type of linked list in which every node contains
some data and a pointer to the next node
Definition of a Node in a singly linked list
• public class Node {
• int data;
• Node next;
• // Constructor to initialize the node with data
• public Node(int data)
• {
• this.data = data;
• this.next = null;
• }
• }
Operations on Singly Linked List
• Traversal
• Searching
• Length
• Insertion:
– Insert at the beginning
– Insert at the end
– Insert at a specific position
• Deletion:
– Delete from the beginning
– Delete from the end
– Delete a specific node
Traversal
head = head.next;
}
return false;
}
Length
Step-by-step approach:
• Initialize a counter length to 0.
• Start from the head of the list, assign it to current.
• Traverse the list:
– Increment length for each node.
– Move to the next node (current = current->next).
• Return the final value of length.
•
public int findLength(Node head) {
int length = 0;
Node current = head;
while (current != null) {
length++;
current = current.next;
}
return length;
}
Insertion
return head;
}
Deletion
Deletion at the Beginning of Singly Linked List:
• To delete the first node, update the head to point to the
second node in the list.
Steps-by-step approach:
• Check if the head is NULL.
– If it is, return NULL (the list is empty).
• Store the current head node in a temporary variable temp.
• Move the head pointer to the next node.
• Delete the temporary node.
• Return the new head of the linked list.
• static Node removeFirstNode(Node head)
• {
• if (head == null)
• return null;
• Node temp = head;
• head = head.next;
• return head;
• }
Deletion at the End of Singly Linked List:
• To delete the last node, traverse the list until the second-to-last
node and update its next field to None.
Step-by-step approach:
• Check if the head is NULL.
– If it is, return NULL (the list is empty).
• Check if the head's next is NULL (only one node in the list).
– If true, delete the head and return NULL.
• Traverse the list to find the second last node (second_last).
• Delete the last node (the node after second_last).
• Set the next pointer of the second last node to NULL.
• Return the head of the linked list.
• Node removeLastNode(Node head)
• // Remove the last node
• {
• second_last.next = null;
• if (head == null)
• return null;
• // Return the modified list
• if (head.next == null) {
• return head;
• head = null;
• }
• return null;
• }
• Node second_last = head;
• while (second_last.next.next != null)
• second_last = second_last.next;
Deletion at a Specific Position of Singly Linked List:
• To delete a node at a specific position, traverse the list to the
desired position, update the links to bypass the node to be deleted.
Step-by-step approach:
• Check if the list is empty or the position is invalid, return if so.
• If the head needs to be deleted, update the head and delete the
node.
• Traverse to the node before the position to be deleted.
• If the position is out of range, return.
• Store the node to be deleted.
• Update the links to bypass the node.
• Delete the stored node.
• public void deleteAtPosition(Node head, int position)
• {
• if (head == null || position < 1) {
• return;
• }
• if (position == 1) {
• Node temp = head;
• head = head.next;
• temp = null;
• return;
• }
• // Traverse to the node before the position to be
• // deleted
• Node current = head;
• for (int i = 1; i < position - 1 && current != null;
• i++) {
• current = current.next;
• }
• // If the position is out of range
• if (current == null || current.next == null) {
• return;
• }
• // Store the node to be deleted
• Node temp = current.next;
• // Update the links to bypass the node to be deleted
• current.next = current.next.next;
• head.next = second;
• second.prev = head;
• second.next = third;
• third.prev = second;
• System.out.println("Forward Traversal:");
• forwardTraversal(head);
• System.out.println("Backward Traversal:");
• backwardTraversal(third);
• }
• }
• static void backwardTraversal(Node tail) {
• Node curr = tail;
• // Continue until the current node is
• // null (end of the list)
• while (curr != null) {
• // Output data of the current node
• System.out.print(curr.data + " ");
• // Move to the previous node
• curr = curr.prev;
• }
• // Print newline after traversal
• System.out.println();
• }
• public static void main(String[] args) {
• // Sample usage of the doubly linked
• // list and traversal functions
• Node head = new Node(1);
• Node second = new Node(2);
• Node third = new Node(3);
• head.next = second;
• second.prev = head;
• second.next = third;
• third.prev = second;
• System.out.println("Forward Traversal:");
• forwardTraversal(head);
• System.out.println("Backward Traversal:");
• backwardTraversal(third);
• }
• }
• Finding Length of Doubly Linked List
• To find the length of doubly list, we can use the
following steps:
• Start at the head of the list.
• Traverse through the list, counting each node visited.
• Return the total count of nodes as the length of the
list.
• class GfG {
• // Function to find the length of
• // a doubly linked list
• static int FindLength(Node head) {
• int count = 0;
• for (Node cur = head; cur != null; cur = cur.next)
• count++;
• return count;
• }
• Insertion at the Beginning in Doubly Linked List
• To insert a new node at the beginning of the doubly list, we can
use the following steps:
• Create a new node, say new_node with the given data and set its
previous pointer to null, new_node->prev = NULL.
• Set the next pointer of new_node to current head, new_node-
>next = head.
• If the linked list is not empty, update the previous pointer of the
current head to new_node, head->prev = new_node.
• Return new_node as the head of the updated linked list.
class GfG {
static Node insertBegin(Node head, int data) {
Node new_node = new Node(data);
new_node.next = head;
if (head != null) {
head.prev = new_node;
}
return new_node;
}
• // Print the doubly linked list
• static void printList(Node head) {
• Node curr = head;
• while (curr != null) {
• System.out.print(curr.data + " ");
• curr = curr.next;
• }
• System.out.println();
• }
• public static void main(String[] args) {
• Node head = new Node(2);
• head.next = new Node(3);
• head.next.prev = head;
• head.next.next = new Node(4);
• head.next.next.prev = head.next;
• System.out.print("Original Linked List: ");
• printList(head);
• head = insertBegin(head, 1);
• System.out.print(
• "After inserting Node 1 at the front: ");
• printList(head);
• }
• }
• public static void main(String[] args) {
• Node head = new Node(2);
• head.next = new Node(3);
• head.next.prev = head;
• head.next.next = new Node(4);
• head.next.next.prev = head.next;
• System.out.print("Original Linked List: ");
• printList(head);
• head = insertBegin(head, 1);
• System.out.print(
• "After inserting Node 1 at the front: ");
• printList(head);}}
• Insertion at the End of Doubly Linked List
• To insert a new node at the end of the doubly linked list, we can use the
following steps:
• Allocate memory for a new node and assign the provided value to its data
field.
• Initialize the next pointer of the new node to nullptr.
• If the list is empty:
– Set the previous pointer of the new node to nullptr.
– Update the head pointer to point to the new node.
• If the list is not empty:
– Traverse the list starting from the head to reach the last node.
– Set the next pointer of the last node to point to the new node.
– Set the previous pointer of the new node to point to the last node.
// Java Program to insert a node at the end of
// doubly linked list
class Node {
int data;
Node next, prev;
Node(int newData) {
data = newData;
next = prev = null;
}
}
class GFG {
public static Node insertEnd(Node head, int newData) {
Node newNode = new Node(newData);
if (head == null) {
head = newNode;
}
else {
Node curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = newNode;
newNode.prev = curr;
}
return head;
}
• public static void printList(Node head) {
• Node curr = head;
• while (curr != null) {
• System.out.print(curr.data + " ");
• curr = curr.next;
• }
• System.out.println();
• }
•
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
System.out.println("Original Linked List: ");
printList(head);
System.out.println(
"Inserting Node with data 4 at the end: ");
int data = 4;
head = insertEnd(head, data);
printList(head);
}
}
• Insertion at a Specific Position in Doubly Linked List
• To insert a node at a specific Position in doubly linked
list, we can use the following steps:
•
• To insert a new node at a specific position,
• If position = 1, create a new node and make it the head of the linked list
and return it.
• Otherwise, traverse the list to reach the node at position – 1, say curr.
• If the position is valid, create a new node with given data, say new_node.
• Update the next pointer of new node to the next of current node and prev
pointer of new node to current node, new_node->next = curr-
>next and new_node->prev = curr.
• Similarly, update next pointer of current node to the new node, curr->next
= new_node.
• If the new node is not the last node, update prev pointer of new node’s
next to the new node, new_node->next->prev = new_node.
• Be
• class GFG {
• public static Node insertAtPosition(Node head, int pos, int new_data) {
• Node new_node = new Node(new_data);
• if (pos == 1) {
• new_node.next = head;
• if (head != null) {
• head.prev = new_node;
• }
• head = delHead(head);
• printList(head);
• }
• }
• public static void main(String[] args) {
• Node head = new Node(1);
• head.next = new Node(2);
• head.next.prev = head;
• head.next.next = new Node(3);
• head.next.next.prev = head.next;
• head = delHead(head);
• printList(head);
• }
• }
• Deletion at the End in Doubly Linked List
• To delete a node at the end in doubly linked list, we can use
the following steps:
• Check if the doubly linked list is empty. If it is empty, then
there is nothing to delete.
• If the list is not empty, then move to the last node of the
doubly linked list, say curr.
• Update the second-to-last node’s next pointer to NULL, curr-
>prev->next = NULL.
• Free the memory allocated for the node that was deleted
• // Java Program to delete a node from the
• // end of Doubly Linked List
• class Node {
• int data;
• Node prev;
• Node next;
• Node(int d) {
• data = d;
• prev = null;
• next = null;
• }
• }
public class GfG {
• static Node delLast(Node head) {
• if (head == null) {
• return null;
• }
• if (head.next == null) {
• return null;
• }
• // Traverse to the last node
• Node curr = head;
• while (curr.next != null) {
• curr = curr.next;
• }
• head = delLast(head);
• printList(head);
• }
• }
• public static void main(String[] args) {
• // Create a hardcoded doubly linked list:
• // 1 <-> 2 <-> 3
• Node head = new Node(1);
• head.next = new Node(2);
• head.next.prev = head;
• head.next.next = new Node(3);
• head.next.next.prev = head.next;
• head = delLast(head);
• printList(head);
• }
• }
• Deletion at a specific position in Doubly Linked List
• To delete a node before a specific node in a doubly linked list, we can use the
following steps:
• Initialize a variable , say curr points to the node with the key value in the linked list.
• if found , check if curr->prev is not NULL.
– If it’s NULL, the node to be deleted is the head node, so there is no node to
delete before it.
– else , set a pointer nodeDelete to curr->prev, which is the node to be deleted.
– Update curr->prev to point to nodeDelete ->prev.
– If nodeDelete ->prev is not NULL, update nodeDelete->prev->next point
to curr.
• Delete nodeDelete to free memory.
• // Java Program to delete a node before a given node of
• // doubly linked list
• class Node {
• int data;
• Node next;
• Node prev;
• Node(int x) {
• data = x;
• next = null;
• prev = null;
• }
• }
• class GfG {
• // Function to delete a node before a given node in a
• // doubly linked list
• static Node deleteBefore(Node head, int key) {
• Node curr = head;
• // Node to be deleted
• Node nodeToDelete = curr.prev;
• return head;
• }