diff --git a/Java/singly-linked-list/LengthOfLinkedList.java b/Java/singly-linked-list/LengthOfLinkedList.java new file mode 100644 index 0000000..dcfd77c --- /dev/null +++ b/Java/singly-linked-list/LengthOfLinkedList.java @@ -0,0 +1,34 @@ +class Node{ + int data; + Node next; + Node(int data1, Node next1){ + this.data=data1; + this.next=next1; + } + Node(int data1){ + this.data=data1; + this.next=null; + } +}; +public class LengthOfLinkedList { + // Function to calculate the length of a linked list + private static int lengthOfLL(Node head){ + int cnt=0; + Node temp=head; + // Traverse the linked list and count nodes + while(temp!=null){ + temp = temp.next; + cnt++;// increment cnt for every node traversed + } + return cnt; + } + public static void main(String[] args) { + int[]arr={2,5,8,7}; + Node head = new Node(arr[0]); + head.next= new Node(arr[1]); + head.next.next= new Node(arr[2]); + head.next.next.next= new Node(arr[3]); + // Print the length of the linked list + System.out.println(lengthofaLL(head)); + } +} \ No newline at end of file diff --git a/Java/singly-linked-list/SearchInLinkedList.java b/Java/singly-linked-list/SearchInLinkedList.java new file mode 100644 index 0000000..0a2528d --- /dev/null +++ b/Java/singly-linked-list/SearchInLinkedList.java @@ -0,0 +1,52 @@ +// Node class represents a node in a linked list +class Node { + int data; // Data stored in the node + Node next; // Reference to the next node in the list + + // Constructor with both data and next node as parameters + Node(int data1, Node next1) { + this.data = data1; + this.next = next1; + } + + // Constructor with only data as a parameter, sets next to null + Node(int data1) { + this.data = data1; + this.next = null; + } +} + +// LinkedList class contains utility methods for linked list operations +public class SearchInLinkedList { + + // Function to check if a given element is present in the linked list + public static int checkifPresent(Node head, int desiredElement) { + Node temp = head; + + // Traverse the linked list + while (temp != null) { + // Check if the current node's data is equal to the desired element + if (temp.data == desiredElement) + return 1; // Return 1 if the element is found + + // Move to the next node + temp = temp.next; + } + + return 0; // Return 0 if the element is not found in the linked list + } + + // Main function + public static void main(String[] args) { + // Create a linked list: 1 -> 2 -> 3 + int[] arr = {1, 2, 3}; + Node head = new Node(arr[0]); + head.next = new Node(arr[1]); + head.next.next = new Node(arr[2]); + + int val = 3; // Element to be checked for presence in the linked list + + // Call the checkifPresent function and print the result + System.out.print(checkifPresent(head, val)); + } +} \ No newline at end of file diff --git a/Java/singly-linked-list/deletelastnode.java b/Java/singly-linked-list/deletelastnode.java new file mode 100644 index 0000000..dd2bbd6 --- /dev/null +++ b/Java/singly-linked-list/deletelastnode.java @@ -0,0 +1,55 @@ +// Node class definition +class Node { + int data; + Node next; + // Constructor with both data and next pointer + Node(int data1, Node next1) { + this.data = data1; + this.next = next1; + } + // Constructor with only data (next pointer set to null) + Node(int data1) { + this.data = data1; + this.next = null; + } +} +// LinkedList class +public class DeleteLastNode { + // Function to delete the tail of the linked list + private static Node deleteTail(Node head) { + // Check if the linked list is empty or has only one node + if (head == null || head.next == null) + return null; + // Create a temporary pointer for traversal + Node temp = head; + // Traverse the list until the second-to-last node + while (temp.next.next != null) { + temp = temp.next; + } + // Nullify the connection from the second-to-last node to delete the last node + temp.next = null; + // Return the updated head of the linked list + return head; + } + // Function to print the linked list + private static void printLL(Node head) { + while (head != null) { + System.out.print(head.data + " "); + head = head.next; + } + } + // Main method + public static void main(String[] args) { + // Initialize an array with integer values + int[] arr = {2, 5, 8, 7}; + // Create the linked list with nodes initialized with array values + Node head = new Node(arr[0]); + head.next = new Node(arr[1]); + head.next.next = new Node(arr[2]); + head.next.next.next = new Node(arr[3]); + // Delete the tail of the linked list + head = deleteTail(head); + // Print the modified linked list + printLL(head); + } +} \ No newline at end of file diff --git a/Java/singly-linked-list/insertathead.java b/Java/singly-linked-list/insertathead.java new file mode 100644 index 0000000..50df85c --- /dev/null +++ b/Java/singly-linked-list/insertathead.java @@ -0,0 +1,53 @@ +import java.util.*; + +// Node class to represent a linked list node +class Node { + public int data; + public Node next; + + // Constructor with both data and next node + public Node(int data1, Node next1) { + data = data1; + next = next1; + } + + // Constructor with only data (assuming next is initially null) + public Node(int data1) { + data = data1; + next = null; + } +} + +public class InsertAtHead { + // Function to print the linked list + public static void printLL(Node head) { + while (head != null) { + System.out.print(head.data + " "); + head = head.next; + } + } + + // Function to insert a new node at the head of the linked list + public static Node insertHead(Node head, int val) { + Node temp = new Node(val, head); + return temp; + } + + public static void main(String[] args) { + // Sample array and value for insertion + List arr = Arrays.asList(12, 8, 5, 7); + int val = 100; + + // Creating a linked list with initial elements from the array + Node head = new Node(arr.get(0)); + head.next = new Node(arr.get(1)); + head.next.next = new Node(arr.get(2)); + head.next.next.next = new Node(arr.get(3)); + + // Inserting a new node at the head of the linked list + head = insertHead(head, val); + + // Printing the linked list + printLL(head); + } +} \ No newline at end of file