Question : Given a Linked List and a number n, write a program that find the value at the n’th node from end of the Linked List. Method 1 – Use length of linked list 1. Calculate the length of Linked List. Follow this tutorial Find Length of a Linked List data 2. Print Read More…
Category: Data structure
data structures
Find the middle of a given linked list data structure
Question: In given a Linked List, find the middle of the list and print the number. Input : -> 4-> 2-> 7-> 9-> 1 Output : 7 Before starting you have to must know about Linked list and how to create it and Inserting node (data) in LinkedList. There is many approach to find middle number Read More…
Inserting a new node in a linked list data structure
Inserting new node in linked list can do 3 ways. At start of Linked list In the middle of Linked list At the end of Linked list Before starting you have to must know about Linked list and how to create it. 1. At Start of Linked list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package in.eyehunt.data.struc; public class LinkedList { Node head; // head of list // Linked list Node. class Node { int data; Node next; // Parameterized constructor Node(int d) { data = d; next = null; } } // method to add new node in start void push(int n) { //create new node Node newNode = new Node(n); // next node is head newNode.next = head; // move had point to new node head = newNode; } void printAllNodes() { Node node = head; while (node != null) { System.out.print("-> "+node.data); node = node.next; } } public static void main(String a[]) { //create a simple linked list with 4 nodes LinkedList linkedList = new LinkedList(); linkedList.push(1); linkedList.push(4); linkedList.push(3); linkedList.push(2); linkedList.printAllNodes(); } } |
2. In the Middle of Linked Read More…
Find Length of a Linked List data structure (Iterative and Recursive)
How to Find Length of Linked List data structure? Linked list A linked list is a linear data structure where each element is a separate object.Counting a node in linked list is important in programming when we are using linked list data structure. It will help you solve many problems like fining nth node from Read More…