public Node merge(Node head) { if (head == null || head.next == null) { return head; } Node newHead = merge(head.next); if (head.val == newHead.val) { head.counter = head.counter + newHead.counter; head.next = newHead.next; } return head; }
public static void main(String[] args) { ListNodeWithCounter test = new ListNodeWithCounter(); Node head = new Node(1, 1); head.next = new Node(1, 3); head.next.next = new Node(4, 2); head.next.next.next = new Node(3, 1); head.next.next.next.next = new Node(3, 1); Node node = test.merge(head); while (node != null) { System.out.print(node.val + ", " + node.counter + "->"); node = node.next; } }
private Node chooseNextNode() { int max = Integer.MAX_VALUE; Node tmpnode = null; for (Node node : this.allNodes) { if (!node.isVisited() && node.getDistance() < max && node.distance != -1) { max = node.getDistance(); tmpnode = node; } } return tmpnode; }
private void createShortestPath() { ArrayList<Integer> ar = new ArrayList<Integer>(); Node aktnode = this.allNodes[this.endNode]; ar.add(aktnode.id); do { aktnode = aktnode.getPredecessor(); ar.add(aktnode.id); } while (aktnode.getId() != this.startNode); this.path = new int[ar.size()]; for (int i = 0; i < this.path.length; i++) { this.path[i] = ar.get((ar.size() - 1) - i); } }
private void distanz_update() { Node aktnode = this.allNodes[this.startNode]; do { // Solange es noch unbesuchte Knoten gibt aktnode.setVisited(); // Alle Nachbarn des aktuellen Knotens for (Node node : aktnode.getNeighbors()) { if (node.getDistance() > aktnode.getDistance() + graph.getDistance(aktnode.getId(), node.getId()) || node.getDistance() == -1) { node.setDistance( aktnode.getDistance() + graph.getDistance(aktnode.getId(), node.getId())); node.setPredecessor(aktnode); } } aktnode = chooseNextNode(); } while (unvisitedNodes()); }