From f688759236e9e6de2b477fe4a07881950233e818 Mon Sep 17 00:00:00 2001 From: Atharv Mudse Date: Mon, 6 Oct 2025 23:21:28 +0530 Subject: [PATCH 1/2] Added Linked List in java with basic opertiaons(search,delete,insert,length) --- Java/singly-linked-list/deletelastnode.java | 55 +++++++++++++++++++ Java/singly-linked-list/insertathead.java | 53 ++++++++++++++++++ .../lengthodlinkedlist.java | 34 ++++++++++++ .../searchinanlinkedlist.java | 52 ++++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 Java/singly-linked-list/deletelastnode.java create mode 100644 Java/singly-linked-list/insertathead.java create mode 100644 Java/singly-linked-list/lengthodlinkedlist.java create mode 100644 Java/singly-linked-list/searchinanlinkedlist.java diff --git a/Java/singly-linked-list/deletelastnode.java b/Java/singly-linked-list/deletelastnode.java new file mode 100644 index 0000000..963c472 --- /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..5da14f1 --- /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 diff --git a/Java/singly-linked-list/lengthodlinkedlist.java b/Java/singly-linked-list/lengthodlinkedlist.java new file mode 100644 index 0000000..4f6ba51 --- /dev/null +++ b/Java/singly-linked-list/lengthodlinkedlist.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 lengthodlinkedlist { + // Function to calculate the length of a linked list + private static int lengthofaLL(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/searchinanlinkedlist.java b/Java/singly-linked-list/searchinanlinkedlist.java new file mode 100644 index 0000000..12a1eb1 --- /dev/null +++ b/Java/singly-linked-list/searchinanlinkedlist.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 searchinanlinkedlist { + + // 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 From 274196f9f69414999d4a46a44add1b04e91de1b2 Mon Sep 17 00:00:00 2001 From: Atharv Mudse Date: Tue, 7 Oct 2025 00:08:55 +0530 Subject: [PATCH 2/2] Fixed the naming pattern --- .../{lengthodlinkedlist.java => LengthOfLinkedList.java} | 4 ++-- .../{searchinanlinkedlist.java => SearchInLinkedList.java} | 2 +- Java/singly-linked-list/deletelastnode.java | 2 +- Java/singly-linked-list/insertathead.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename Java/singly-linked-list/{lengthodlinkedlist.java => LengthOfLinkedList.java} (91%) rename Java/singly-linked-list/{searchinanlinkedlist.java => SearchInLinkedList.java} (97%) diff --git a/Java/singly-linked-list/lengthodlinkedlist.java b/Java/singly-linked-list/LengthOfLinkedList.java similarity index 91% rename from Java/singly-linked-list/lengthodlinkedlist.java rename to Java/singly-linked-list/LengthOfLinkedList.java index 4f6ba51..dcfd77c 100644 --- a/Java/singly-linked-list/lengthodlinkedlist.java +++ b/Java/singly-linked-list/LengthOfLinkedList.java @@ -10,9 +10,9 @@ class Node{ this.next=null; } }; -public class lengthodlinkedlist { +public class LengthOfLinkedList { // Function to calculate the length of a linked list - private static int lengthofaLL(Node head){ + private static int lengthOfLL(Node head){ int cnt=0; Node temp=head; // Traverse the linked list and count nodes diff --git a/Java/singly-linked-list/searchinanlinkedlist.java b/Java/singly-linked-list/SearchInLinkedList.java similarity index 97% rename from Java/singly-linked-list/searchinanlinkedlist.java rename to Java/singly-linked-list/SearchInLinkedList.java index 12a1eb1..0a2528d 100644 --- a/Java/singly-linked-list/searchinanlinkedlist.java +++ b/Java/singly-linked-list/SearchInLinkedList.java @@ -17,7 +17,7 @@ class Node { } // LinkedList class contains utility methods for linked list operations -public class searchinanlinkedlist { +public class SearchInLinkedList { // Function to check if a given element is present in the linked list public static int checkifPresent(Node head, int desiredElement) { diff --git a/Java/singly-linked-list/deletelastnode.java b/Java/singly-linked-list/deletelastnode.java index 963c472..dd2bbd6 100644 --- a/Java/singly-linked-list/deletelastnode.java +++ b/Java/singly-linked-list/deletelastnode.java @@ -14,7 +14,7 @@ class Node { } } // LinkedList class -public class deletelastnode { +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 diff --git a/Java/singly-linked-list/insertathead.java b/Java/singly-linked-list/insertathead.java index 5da14f1..50df85c 100644 --- a/Java/singly-linked-list/insertathead.java +++ b/Java/singly-linked-list/insertathead.java @@ -18,7 +18,7 @@ public Node(int data1) { } } -public class insertathead { +public class InsertAtHead { // Function to print the linked list public static void printLL(Node head) { while (head != null) {