Question
java


singly linked list write a routine, which will travel through the list second and third list by taking every 2nd node from th
0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA code for above problem-

import java.util.*;

class Create_second_third_list{
  
public static void formlists(LinkedList<Integer> str1) {
  
//creating second and third linked list
LinkedList<Integer> str2 = new LinkedList<Integer>();
LinkedList<Integer> str3 = new LinkedList<Integer>();
  
int k=1; // counter for getting every second and third node
for(int num=0; num<str1.size();)
{
   if(k==2){ // inserting every 2nd node in 2nd list
   str2.add(str1.get(num));
   str1.remove(num);
   k++;
   }
   else if(k==3){ // inserting every 3rd node in 3rd list
   str3.add(str1.get(num));
   str1.remove(num);
   k=1;
   }
   else{ // keeping the rest nodes in 1st list
   k++;
   num++;
   }
}
  
System.out.println();
  
//displaying 1st list
System.out.print("First Linked List- ");
for(int num=0; num<str1.size(); num++)
{
   System.out.print(str1.get(num)+" ");
}
System.out.println();
  
//displaying 2nd list
System.out.print("Second Linked List- ");
for(int num=0; num<str2.size(); num++)
{
   System.out.print(str2.get(num)+" ");
}
System.out.println();
  
//displaying 3rd list
System.out.print("Third Linked List- ");
for(int num=0; num<str3.size(); num++)
{
   System.out.print(str3.get(num)+" ");
}
System.out.println();
  
}
}

class Main {

public static void main (String [] args) {
  
Scanner scnr = new Scanner(System.in);

System.out.print("Enter the number of node in first linked list-");
int n=scnr.nextInt(); // taking input 'n'
  
LinkedList<Integer> str1 = new LinkedList<Integer>();
  
for(int i=1;i<=n;i++){ // inserting 1 to n in first list
str1.add(i); // creating the first linked list
}
  
// creating the object
Create_second_third_list C= new Create_second_third_list();
  
// calling the method
C.formlists(str1);

}
}


Code-

import java.util.*; class Create_second_third_list{ public static void formlists(LinkedList<Integer> str1) { //creating seconelse // keeping the rest nodes in 1st list k+ num+ } System.out.println(); //displaying 1st list System.out.print(First Link//displaying 3rd list System.out.print(Third Linked List- ); for (int num 0; num<str3.size(); num++) { System.out.print(strfor (int i 1;i<=n;i++){ strl.add(i); / creating the first linked list /creating the object Create_second third_list c= new Cr

Input and Output-

inputf.in Enter the number of node in first linked list- 10 outputf.in First Linked List- 1 4 7 10 Second Linked List- 2 5 8

If the answer helped then please upvote.And for any queries,please comment.

Add a comment
Know the answer?
Add Answer to:
java singly linked list write a routine, which will travel through the list second and third...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2 Given a pointer to a singly linked list write a routine, which will travel through...

    2 Given a pointer to a singly linked list write a routine, which will travel through t creating a second list by taking every 3nd node from the first list and putting it ir newly created list. (For example: If the original list had 12 nodes it should remai nodes 1, 2, 4, 5, 7, 8, 10 and 11 while the 2nd list would be made up of nodes 3, 6, 12. Briefly explain: A. The advantage of implementing a...

  • Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list...

    Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...

    Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next

  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • 1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head...

    1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head reference refers to the first node contains an Integer with value 4. If Node curr = head; curr= curr.next; are applied to this list, what is the number you can find in the first node? 2) Given a singly linked list contains 6 nodes, which is simply shown as 1->2->3->4->5->6. Assume head refers to the first node (contains 1) on the list. How many...

  • build a singly linked list in C++, input data from keyboard, this list includes 10 nodes....

    build a singly linked list in C++, input data from keyboard, this list includes 10 nodes. The data are "This is my first project in Data Structure and algorithm", the first node is is "This", second "is" and so on.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT