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 =...
using java Consider the following LinkedList that is composed of 4 nodes containing 13, 7, 24,...
using java Consider the following LinkedList that is composed of 4 nodes containing 13, 7, 24, 1. Assume that the Node.value is an int, and the reference to the first value is a Node called n. Write a method that computes the sum of all the values in the nodes of a linked list. For example, your method shall return the sum of all the nodes, in this example shall return 45
Laboratory Tasks public class LinkedList {              Node head;               class Node {    &nbsp
Laboratory Tasks public class LinkedList {              Node head;               class Node {                       int data;                     Node next;                     Node(int d) {                             data = d;                             next = null;                     }                 } } Complete the above java program by adding the following methods: Part1: isEmpty() checks if the linked list is empty or not. (return type is boolean) printList() prints all data in the linked list. (void method) insertFirst(int newData) add newData at the head of the linked list. (void method) insertLasL(int newData) add newData at...
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.
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
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...
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...
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 }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT