/**
* Converts linked list into a sentence (a single string representation).
* Each word pair is separated by a space. A period (".") is appended after
* the last word. The last link represents the first word in the sentence
* (and vice versa). The partialResult is the partial string constructed
* from earlier links. This partialResult is initially an empty string.
*/
public String getReversedSentence(String partialResult) {
if (next==null) {
partialResult+=this.word;
return partialResult + ".";
}
else{
partialResult=next.getReversedSentence(partialResult) + this.word;
return partialResult;
}
}
Everything is working fine, except for the period (and the spaces, but Im not worrying about that yet). I cant get the period to be placed properly.
Here's the test it's failing:
public void testGetReversedSentence() {
LinkedList tail = new LinkedList("not",null);
LinkedList middle = new LinkedList("too",tail);
LinkedList head = new LinkedList("tricky",middle);
assertEquals("not.",tail.getReversedSentence(""));
assertEquals("not too tricky.",head.getReversedSentence(""));
It comes up with not.too tricky instead of not too tricky.
Edit: Contrusctor
public LinkedList(String word, LinkedList next) {
this.word = word;
this.next = next;
}
Any hints?