Jeg saa dit spergsmaal foer dette.
Og det var lidt trist, fordi jeg maatte erkende, at jeg ikke laengere er i stand til at hjaelpe med den slags spoergsmaal. Jeg goer simpelthen tingene for anderledes.
Hvis du vil se en anden tilgang til problemstillingen, saa se foelgende:
public class LinkedList<T> {
private static class ListNode<T> {
public ListNode<T> next;
public T data;
public ListNode(T data, ListNode<T> next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}
@FunctionalInterface
private static interface NodeAggregator<T,TR> {
public TR aggregate(TR prevval, ListNode<T> node);
}
@FunctionalInterface
private static interface NodeProcessor<T> {
public void process(ListNode<T> node);
}
@FunctionalInterface
private static interface NodePairProcessor<T> {
public void process(ListNode<T> node1, ListNode<T> node2);
}
@FunctionalInterface
private static interface Combiner<T> {
public T combine(T val1, T val2);
}
private ListNode<T> head;
private void insertAfter(ListNode<T> prevnode, T data) {
prevnode.next = new ListNode<T>(data, prevnode.next);
}
private void removeAfter(ListNode<T> prevnode) {
prevnode.next = prevnode.next.next;
}
private ListNode<T> last() {
ListNode<T> curr = head;
while(curr.next != null) {
curr = curr.next;
}
return curr;
}
private <TR> TR aggregateAll(TR initial, NodeAggregator<T,TR> aggr) {
TR result = initial;
ListNode<T> curr = head;
while(curr != null) {
result = aggr.aggregate(result, curr);
curr = curr.next;
}
return result;
}
private void processAll(NodeProcessor<T> proc) {
ListNode<T> curr = head;
while(curr != null) {
ListNode<T> next = curr.next; // to handle the case where curr.next gets modified by the processor
proc.process(curr);
curr = next;
}
}
private void processAll(NodePairProcessor<T> proc) {
ListNode<T> curr = head;
while(curr != null && curr.next != null) {
ListNode<T> next = curr.next.next; // to handle the case where curr.next.next gets modified by the processor
proc.process(curr, curr.next);
curr = next;
}
}
public LinkedList() {
head = null;
}
public void append(T data) {
if(head == null) {
head = new ListNode<T>(data, null);
} else {
insertAfter(last(), data);
}
}
@Override
public String toString() {
return "{" + aggregateAll("", (prevval,node) -> prevval + " " + node.toString()) + " }";
}
public void stretch(final int n) {
processAll(node -> { for(int i = 1; i < n; i++) insertAfter(node, node.data); });
}
public void compress(Combiner<T> comb) {
processAll((node1, node2) -> { node1.data = comb.combine(node1.data, node2.data); removeAfter(node1); } );
}
public static void main(String[] args) {
LinkedList<Integer> lst = new LinkedList<Integer>();
lst.append(2);
lst.append(4);
lst.append(6);
System.out.println(lst);
lst.stretch(3);
System.out.println(lst);
lst.compress((val1, val2) -> val1 + val2);
System.out.println(lst);
}
}
Noter:
* jeg vil bestemt fraraade dig at aflevere noget som dette - det vil signalere fusk
* det er kodet til Java 8, men kan nemt konverteres til aeldre Java versioner