Java Program To Delete Alternate Nodes Of A Linked List Last Updated : 31 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1 (Iterative): Keep track of previous of the node to be deleted. First, change the next link of the previous node and iteratively move to the next node. Java // Java program to delete alternate // nodes of a linked list class LinkedList { // Head of list Node head; // Linked list Node class Node { int data; Node next; Node(int d) { data = d; next = null; } } void deleteAlt() { if (head == null) return; Node prev = head; Node now = head.next; while (prev != null && now != null) { // Change next link of previous node prev.next = now.next; // Free node now = null; // Update prev and now prev = prev.next; if (prev != null) now = prev.next; } } // Utility functions // Inserts a new Node at front // of the list. public void push(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); // 3. Make next of new Node as head new_node.next = head; // 4. Move the head to point to // new Node head = new_node; } // Function to print linked list void printList() { Node temp = head; while(temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } // Driver code public static void main(String args[]) { LinkedList llist = new LinkedList(); /* Constructed Linked List is 1->2->3->4->5->null */ llist.push(5); llist.push(4); llist.push(3); llist.push(2); llist.push(1); System.out.println( "Linked List before calling deleteAlt() "); llist.printList(); llist.deleteAlt(); System.out.println( "Linked List after calling deleteAlt() "); llist.printList(); } } // This code is contributed by Rajat Mishra Output: List before calling deleteAlt() 1 2 3 4 5 List after calling deleteAlt() 1 3 5 Time Complexity: O(n) where n is the number of nodes in the given Linked List. Auxiliary Space: O(1) because it is using constant space Method 2 (Recursive): Recursive code uses the same approach as method 1. The recursive code is simple and short but causes O(n) recursive function calls for a linked list of size n. Java /* Deletes alternate nodes of a list starting with head */ static Node deleteAlt(Node head) { if (head == null) return; Node node = head.next; if (node == null) return; // Change the next link of head head.next = node.next; /* Recursively call for the new next of head */ head.next = deleteAlt(head.next); } // This code is contributed by Arnab Kundu Time Complexity: O(n) Auxiliary space: O(n) for call stack because using recursion Please refer complete article on Delete alternate nodes of a Linked List for more details! Comment More infoAdvertise with us Next Article Java Program To Delete Alternate Nodes Of A Linked List kartik Follow Improve Article Tags : Linked List Java Java Programs DSA Linked Lists Morgan Stanley +2 More Practice Tags : Morgan StanleyJavaLinked List Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Like