Posts

Showing posts with the label LinkedList

Doubly LinkedList problems

 Greeks problems

Circular LinkedList problems

 Greeks problems

Singly LinkedList problem

 Greeks problems

Intersection point of 2 linkedlist

Greeks Reference1    Greeks Reference2

Find middle node in linkedlist

Greeks reference . /** * Two approaches can be used here : * 1. Take fast and slow pointer. Slow pointer move 1 step ahead and fast pointer moves * 2 steps ahead. When fast pointer reached last then slow reaches to middle * 2. Traverse to floor(size of linkedlist)/2 nodes or Traverse to ceil(size of * linkedlist)/2 nodes based on demand. */ class FindMiddleOfLinkedList { static Node head; static int findMiddle() { Node slow, fast; slow = fast = head; while(fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } static void create(int[] data) { head = new Node(); head.data = data[0]; Node last = head; for(int i = 1; i Output middle element : 3 middle element : 2

Doubly LinkedList Operations

Need to run and correct few errors in output import java.util.*; import java.lang.*; import java.io.*; class SinglyLinkedList { private static Node head; private static Node last; private static void create(int[] data) { // create first Node head = new Node(null, data[0], null); // if only single element if(data.length == 1) { return; } Node last = head; for(int i = 1; i max) { max = current.getData(); } // move next node current = current.getNext(); } return max; } private static int maxRecursive(Node head) { if(head == null) { return Integer.MIN_VALUE; } int max = maxRecursive(head.getNext()); return max > head.getData() ? max : head.getData(); } private static int min() { if(head == null) { return 0; } Node current = head; ...

LinkedList

1. Singly LinkedList  2. Circular LinkedList 3. Doubly LinkedList 4. Circular Doubly LinkedList 5. Applications

Circular LinkedList Operations

No node can have null in circular linked list. class CircularSinglyLinkedList { private static Node head; private static void create(int[] data) { // if data is empty early return if(data.length == 0) { return; } // create first node head = new Node(data[0], null); // point next as self head.setNext(head); Node last = head; // add node 2 to n for(int i = 1; i Output Create list 3 5 7 10 25 8 32 Display list using recursive method 3 5 7 10 25 8 32 Insert first 2 3 5 7 10 25 8 32 Insert after 0th position 1 2 3 5 7 10 25 8 32 Insert last 1 2 3 5 7 10 25 8 32 33 Insert after 34th position 1 2 3 5 7 10 25 8 32 33 34 Delete first : 1 Delete 1st node : 2 Delete 3rd node : 7 Delete last : 34 Delete 34rth node : -1 3 5 10 25 8 32 33

SinglyLinkedList Operations

import java.util.*; import java.lang.*; import java.io.*; class SinglyLinkedList { private static Node head; private static void create(int[] data) { // create first Node head = new Node(data[0], null); // if only single element if(data.length == 1) { return; } Node last = head; for(int i = 1; i max) { max = temp.getData(); } // move next node temp = temp.getNext(); } return max; } private static int maxRecursive(Node head) { if(head == null) { return Integer.MIN_VALUE; } int max = maxRecursive(head.getNext()); return max > head.getData() ? max : head.getData(); } private static int min() { if(head == null) { return 0; } Node temp = head; int min = Integer.MAX_VALUE; while(temp != null) { if(temp.getData() curr...