Question

In: Computer Science

Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the...

Find a node in a LinkedList In Java:

1. (2 pts) Define the nodes in the LinkedList.

2. (2 pts) Create the LinkedList using the ListNode class.

3. (4 pts) Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists.

4. (2 pts) Use these two examples to test your method.

Example 1:

Input: 1 -> 2 -> 3, and target value = 3

Output: 3

Example 2:

Input: 1 -> 2 -> 3, and target value = 4

Output: null

Solutions

Expert Solution

Java code

import java.util.Scanner;

class ListNode{
   int data;
   ListNode next;
  
   public ListNode(int val) {
       data = val;
       next = null;
   }
  
   public int getData() {
       return data;
   }
   public void setData(int val) {
       data = val;
   }
   public ListNode getNext() {
       return next;
   }
   public void setNext(ListNode next) {
       this.next = next;
   }
}

public class Main {
  
   public static void main(String args[]) {
       Scanner scan = new Scanner(System.in);
       int n,num;
      
       System.out.print("Enter number of node in LinkedList: ");
       n = scan.nextInt();
       System.out.println("Enter elements in Linked list");
      
       ListNode root = null;
       ListNode tail = null;
       for(int i=0;i<n;i++) {
           System.out.print("Enter value: ");
           num = scan.nextInt();
           ListNode newNode = new ListNode(num);
           if(root == null) {
               root = newNode;
               tail = root;
           }
           else {
               tail.setNext(newNode);
               tail = newNode;
           }
       }
       System.out.print("Enter value to search: ");
       num = scan.nextInt();
       System.out.println(search(root , num));
   }

   public static Integer search(ListNode root, int num) {
       while(root != null) {
           if(root.getData() == num) return num;
           root = root.getNext();
       }
       return null;
   }
}


Related Solutions

1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
C PROGRAMMING: SHIFT TYPEDEFS INSIDE OF AN ARRAY. I have an array of NODES, NODE nodes[513];...
C PROGRAMMING: SHIFT TYPEDEFS INSIDE OF AN ARRAY. I have an array of NODES, NODE nodes[513]; typedef struct node { int weight;    } NODE; I have a value called int num_nodes which gives the number of positions in that array which are filled. What I need to do: there is a value inside of nodes[0], but there are other values in the node array which can be found at index nodes[256] to nodes[256+num_nodes]. I need to shift all of...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy...
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy node”) that connects the end of the list with the beginning, transforming the list to a circular list Code in c++ The Josephus problem is named after the historian Flavius Josephus, who lived between the years 37 and 100 CE. Josephus was a reluctant leader of the Jewish revolt against the Roman Empire. When it appeared that Josephus and his band were to be...
Prove that the entropy of a node never increases after splitting it into smaller children nodes.
Prove that the entropy of a node never increases after splitting it into smaller children nodes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT