public void diff_cleanupMerge(LinkedList<Diff> diffs) {
    diffs.add(new Diff(Operation.EQUAL, ""));  // Add a dummy entry at the end.
    ListIterator<Diff> pointer = diffs.listIterator();
    int count_delete = 0;
    int count_insert = 0;
    String text_delete = "";
    String text_insert = "";
    Diff thisDiff = pointer.next();
    Diff prevEqual = null;
    int commonlength;
    while (thisDiff != null) {
      switch (thisDiff.operation) {
        case INSERT:
          count_insert++;
          text_insert += thisDiff.text;
          prevEqual = null;
          break;
        case DELETE:
          count_delete++;
          text_delete += thisDiff.text;
          prevEqual = null;
          break;
        case EQUAL:
          if (count_delete + count_insert > 1) {
            boolean both_types = count_delete != 0 && count_insert != 0;
            // Delete the offending records.
            pointer.previous();  // Reverse direction.
            while (count_delete-- > 0) {
              pointer.previous();
              pointer.remove();
            }
            while (count_insert-- > 0) {
              pointer.previous();
              pointer.remove();
            }
            if (both_types) {
              // Factor out any common prefixies.
              commonlength = diff_commonPrefix(text_insert, text_delete);
              if (commonlength != 0) {
                if (pointer.hasPrevious()) {
                  thisDiff = pointer.previous();
                  assert thisDiff.operation == Operation.EQUAL
                      : "Previous diff should have been an equality.";
                  thisDiff.text += text_insert.substring(0, commonlength);
                  pointer.next();
                } else {
                  pointer.add(new Diff(Operation.EQUAL,
                      text_insert.substring(0, commonlength)));
                }
                text_insert = text_insert.substring(commonlength);
                text_delete = text_delete.substring(commonlength);
              }
              // Factor out any common suffixies.
              commonlength = diff_commonSuffix(text_insert, text_delete);
              if (commonlength != 0) {
                thisDiff = pointer.next();
                thisDiff.text = text_insert.substring(text_insert.length()
                    - commonlength) + thisDiff.text;
                text_insert = text_insert.substring(0, text_insert.length()
                    - commonlength);
                text_delete = text_delete.substring(0, text_delete.length()
                    - commonlength);
                pointer.previous();
              }
            }
            // Insert the merged records.
            if (text_delete.length() != 0) {
              pointer.add(new Diff(Operation.DELETE, text_delete));
            }
            if (text_insert.length() != 0) {
              pointer.add(new Diff(Operation.INSERT, text_insert));
            }
            // Step forward to the equality.
            thisDiff = pointer.hasNext() ? pointer.next() : null;
          } else if (prevEqual != null) {
            // Merge this equality with the previous one.
            prevEqual.text += thisDiff.text;
            pointer.remove();
            thisDiff = pointer.previous();
            pointer.next();  // Forward direction
          }
          count_insert = 0;
          count_delete = 0;
          text_delete = "";
          text_insert = "";
          prevEqual = thisDiff;
          break;
      }
      thisDiff = pointer.hasNext() ? pointer.next() : null;
    }
    if (diffs.getLast().text.length() == 0) {
      diffs.removeLast();  // Remove the dummy entry at the end.
    }

    /*
     * Second pass: look for single edits surrounded on both sides by equalities
     * which can be shifted sideways to eliminate an equality.
     * e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
     */
    boolean changes = false;
    // Create a new iterator at the start.
    // (As opposed to walking the current one back.)
    pointer = diffs.listIterator();
    Diff prevDiff = pointer.hasNext() ? pointer.next() : null;
    thisDiff = pointer.hasNext() ? pointer.next() : null;
    Diff nextDiff = pointer.hasNext() ? pointer.next() : null;
    // Intentionally ignore the first and last element (don't need checking).
    while (nextDiff != null) {
      if (prevDiff.operation == Operation.EQUAL &&
          nextDiff.operation == Operation.EQUAL) {
        // This is a single edit surrounded by equalities.
        if (thisDiff.text.endsWith(prevDiff.text)) {
          // Shift the edit over the previous equality.
          thisDiff.text = prevDiff.text
              + thisDiff.text.substring(0, thisDiff.text.length()
              - prevDiff.text.length());
          nextDiff.text = prevDiff.text + nextDiff.text;
          pointer.previous(); // Walk past nextDiff.
          pointer.previous(); // Walk past thisDiff.
          pointer.previous(); // Walk past prevDiff.
          pointer.remove(); // Delete prevDiff.
          pointer.next(); // Walk past thisDiff.
          thisDiff = pointer.next(); // Walk past nextDiff.
          nextDiff = pointer.hasNext() ? pointer.next() : null;
          changes = true;
        } else if (thisDiff.text.startsWith(nextDiff.text)) {
          // Shift the edit over the next equality.
          prevDiff.text += nextDiff.text;
          thisDiff.text = thisDiff.text.substring(nextDiff.text.length())
              + nextDiff.text;
          pointer.remove(); // Delete nextDiff.
          nextDiff = pointer.hasNext() ? pointer.next() : null;
          changes = true;
        }
      }
      prevDiff = thisDiff;
      thisDiff = nextDiff;
      nextDiff = pointer.hasNext() ? pointer.next() : null;
    }
    // If shifts were made, the diff needs reordering and another shift sweep.
    if (changes) {
      diff_cleanupMerge(diffs);
    }
  }
Reorder and merge like edit sections.  Merge equalities.
Any edit section can move as long as it doesn't cross an equality.

@param diffs LinkedList of Diff objects.
src/burp/Utils/diff_match_patch.java