NodeClass in Java
List Implementation Class
Main Class
package single_linkedlist; public class Node { int data; Node next; public Node(int data) { this.data = data; } }
package single_linkedlist; public class LinkedList { public static void listPrintData(Node head) { Node currentNode = head; while(currentNode.next != null) { System.out.print(currentNode.data+" --> "); currentNode = currentNode.next; } System.out.println(currentNode.data); } public static void listPrintMiddleDataModel2(Node head) { Node node1 = head; Node node2 = head; while (node2.next != null) { node1 = node1.next; if(node1.next.next != null) node2 = node1.next.next; else node2.next = null; } System.out.println(node1.data+"Middle "); } }
package single_linkedlist; public class Test { public static void main(String[] args) { Node head = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); //Assign next node to current node head.next = node2; System.out.println("List Data :"); LinkedList.listPrintData(head); System.out.println("List Middle Data 2 :"); LinkedList.listPrintMiddleDataModel2(head); } }
Comments
Post a Comment