Question

Given a linked list of integers, write a method that computes and prints the pairwise sums...

Given a linked list of integers, write a method that computes and prints the pairwise sums which is illustrated below with an example. If the linked list has values 5,3,2,9,3,15,22 from head to tail, the pairwise sums would be 8,5,11,12,18,37 which is the sum of two consecutive values from left to right. Notice the output will be of size one less than the original. You will output the pairwise sums exactly as above on a single line, comma separated. If the original list is of size 0 or 1 then print an appropriate error message. Make sure your code works for a linked list of any size, not just for the example above. Estimate the time-complexity of your algorithm.

This is what I have so far

import java.util.*;

public class PairwiseSums<Integer> extends LinkedList<Integer>
{
public String pairwise(LinkedList<Integer> list) {
LinkedList<Integer> list2=new LinkedList<Integer>();
int a, b;
if(list.size()<2)
return ("The list must have at least two values.");
else {
for (int i=0; i<list.size()-1;i++) {
  
  
}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class PairwiseSums<Integer> extends LinkedList<Integer>
{

public String pairwise(LinkedList<Integer> list) {
LinkedList<Integer> list2=new LinkedList<Integer>();
int a, b;
String s="";
if(list.size()<2)
return ("The list must have at least two values.");
else {
for (int i=0; i<list.size()-1;i++) {
int n = list.get(i)+list.get(i+1);
list2.add(n);//adding to list
if(i==list.size()-2)
s=s+Integer.toString(n);
else
s=s+Integer.toString(n)+",";
  
}


}
return s;
}
  

}
  
//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.

Add a comment
Know the answer?
Add Answer to:
Given a linked list of integers, write a method that computes and prints the pairwise sums...
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
  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • I have a Java Data Structures project and we're creating a linked list and an iterator...

    I have a Java Data Structures project and we're creating a linked list and an iterator for that linked list. The add method for the linked list has to be O(1). This is my code public class MyLinkedList extends CS20bLinkedList implements Iterable<Integer> {        @Override      public void add(int value){                        Node tail = head;            if(head == null){                        head = new Node(value);             //System.out.println("Very Fun! "+head.value);                        return;                }else{           ...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as...

    Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as a new list. 1) Implement a method: mergeTwoLists(…){…} 2) Use the LinkedList library in Java 3) Test your method. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 ````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` import java.util.*; public class Inclass1 { public static void main(String[] args){ //Question 5.2 LinkedList l1 = new LinkedList<>(); LinkedList l2 = new LinkedList<>(); l1.add(1); l1.add(2); l1.add(10); l2.add(1); l2.add(3); l2.add(4); LinkedList l3 = mergeTwoLists(l1, l2); System.out.println("Question \n******************************"); for(int i...

  • Please answer this programming question as fast as possible. Will upvote Predict the output of this...

    Please answer this programming question as fast as possible. Will upvote Predict the output of this next question. ookmarks People /courses/34463/quizzes/213587/take 15 pts Question 1 Let class Hugelnt be implemented in the following way. In this class, digits is a linked list that contains the digits of an integer that can be very large and have thousands of digits public class Hugelnt public LinkedList (Integer) digits; public Hugelnt0 digits - new LinkedList (Integer) O: public void printWithThousandsSeperator) // your code...

  • Please make sure the code works perfectly and is accurate. This is high-stake assignment. I will...

    Please make sure the code works perfectly and is accurate. This is high-stake assignment. I will rate highly, thank you very much for saving my life. Question: Create a Java program that extends the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would have these methods: public class ExtLinkedList<E> extends LinkedList<E> { public ExtLinkedList<E>subList() { .... (stuff goes here) } } which returns the values stored at index 0,3,6,9, … of the list. If given an empty list it should simply...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

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