Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node{ Integer body; Node nextNode; Node(Integer value){ body = value; } } private void insertInMiddle(Integer value){ head = start; if(start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while(head.body < value){ if(head.nextNode == null || head.nextNode.body >= value){ Node newNode = new Node(value); newNode.nextNode = head.nextNode; head.nextNode = newNode; break; } head = head.nextNode; } } private void traverse(){ head = start; while(head != null){ System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args){ LinkedList ll = new LinkedList(); ll.insertInMiddle(5); ll.insertInMiddle(10); ll.insertInMiddle(15); ll.insertInMiddle(7); ll.traverse(); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  LinkedList  Data structures  Algorithm | ||||
Related Questions | ||||
What is the difference between ArrayList and LinkedList ? | ||||
If you are given a choice to use either ArrayList and LinkedList, Which one would you use and Why ? | ||||
Is this a valid initialization ? Explain. Collection<Collection> collection = new LinkedList<LinkedList>(); | ||||
Difference between ArrayList and LinkedList ? | ||||
Find the third last element in a linked list ? | ||||
Write a program for LinkedList, with method to append node and traversing the list ? | ||||
Write method to delete Node from a LinkedList. | ||||
Difference between List and LinkedList ? | ||||
Is it legal to initialize List like this ? | ||||