Write a method named Csv ToReverseList() that a) takes in a String containing a
ID: 3826587 • Letter: W
Question
Write a method named Csv ToReverseList() that a) takes in a String containing a comma-separated list of words, b) builds a linear, singly-linked list of the words in reverse order, and c) returns a reference to the top of the new list. For example, if CsvToList("Me, You, Them, The Others") is called, it will return a reference to the first node("The Others") of the following linked list:: "The Others" rightarrow "Them" rightarrow "You" rightarrow "Me" Note that the node text should not contain commas or any leading or trailing Here is the definition of the Node class: class Node {String text; Node next; public Node (String text, Node next) {this.text = text; this next} Your method must be no more than 15 lines long. Here is the definition of you method's first line: public static Node CsvToReverseList (String Csv) {Explanation / Answer
Hi, Please find my implementation:
class Node {
String text;
Node next;
public Node(String text, Node next) {
this.text = text;
this.next = next;
}
}
public static Node csvReverseList(String csv){
// base case
if(csv == null || csv.trim().equals("")){
return null;
}
// splitting csv string by comma
String items[] = csv.split("\s+");
Node head = null;
// traversing items array in reverse order
for(int i=items.length-1; i>=0; i--){
Node newNode = new Node(items[i], head);
head = newNode;
}
return head;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.