Question

In: Computer Science

Given a singly linked list that contains a sequence of integers, write a method that loop...

Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result.

code needed in java! thanks in advance!

Solutions

Expert Solution

Given:-

-Given a singly linked list that contains a sequence of integers,

-write a method that loop through each element in this singly linked list with O(n) time complexity, and let each element multiply 6,

-return the result.

Explanation:-

  • You have to check the method printList in this method all elements are traverse with o(n) time complexity and multiply with 6.
  • in PrintList method also result in return in the form of all elements multiply with 6.
  • if any doubt the comment is below...
import java.io.*;
public class LinkedList { 

 Node head;                     // head of the list 

 // Linked list Node. 
 // This inner class is made static 
 // so that main() can access it 
 
 static class Node { 

     int data; 
     Node next; 

     // Constructor 
     Node(int d) 
     { 
         data = d; 
         next = null; 
     } 
 } 

 
 // Method to insert a new node 
 public static LinkedList insert(LinkedList list, int data) 
 { 
     // Create a new node with given data 
     Node new_node = new Node(data); 
     new_node.next = null; 

     // If the Linked List is empty, 
     // then make the new node as head 
     if (list.head == null) { 
         list.head = new_node; 
     } 
     else { 
         // Else traverse till the last node 
         // and insert the new_node there 
         Node last = list.head; 
         while (last.next != null) { 
             last = last.next; 
         } 

         // Insert the new_node at last node 
         last.next = new_node; 
     } 

     // Return the list by head 
     return list; 
 } 

 // Method to print the LinkedList and multiply

 public static void printList(LinkedList list) 
 { 
     Node currNode = list.head; 

     System.out.print("LinkedList with multiply 6 :- "); 
     

     // Traverse through the LinkedList with o(n) complexity...
     while (currNode != null) 
     { 
                       // Print the data at the current node with multiply each number with 6 and print.
         System.out.print((currNode.data)*6 + " "); 
         
         // Go to next node 
         currNode = currNode.next; 
     } 
 } 

 // main method
 public static void main(String[] args) 
 { 
     /* Start with the empty list. */
     LinkedList list = new LinkedList(); 

     // Insert the values 
     list = insert(list, 1); 
     list = insert(list, 2); 
     list = insert(list, 3); 
     list = insert(list, 4); 
     list = insert(list, 5); 
     list = insert(list, 6); 
     list = insert(list, 7); 
     list = insert(list, 8); 

     printList(list);  //print list  
 } 
} 

Output:-


Related Solutions

Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
HI i will write user's manual for a doubly/singly linked list , How can i write...
HI i will write user's manual for a doubly/singly linked list , How can i write User's manual ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT