4-Linked Lists/07. Linked List Problems_/JavaLinkedListDataStructures/JavaLinkedListDataStructures/LinkedList.java
public LinkedList<T> sortedMergeList(LinkedList otherList) {
        if (otherList == null) {
            return this;
        } else if (head == null) {
            return otherList;
        } else {
            Node<T> curr1 = otherList.head;
            Node<T> curr2 = head;
            LinkedList<T> sortedList = new LinkedList<T>();

            while (curr1 != null || curr2 != null) {
                if (curr2 == null ||
                        (curr1 != null && curr1.getData().compareTo(curr2.getData()) < 0)) {
                  sortedList.addNode(curr1.getData());
                  curr1 = curr1.getNext();
                }
                else {
                  sortedList.addNode(curr2.getData());
                  curr2 = curr2.getNext();
                }
            }

            return  sortedList;
        }
    }
/**
     * Create a new sorted list which is the merged from two original sorted lists.
     * Assume the lists are sorted in ascending order.
     */
Create a new sorted list which is the merged from two original sorted lists.
Assume the lists are sorted in ascending order.
Create a new sorted list which is the merged from two original sorted lists . Assume the lists are sorted in ascending order .
public LinkedList<T> sortedMergeList(LinkedList otherList) {
        if (otherList == null) {
            return this;
        } else if (head == null) {
            return otherList;
        } else {
            Node<T> curr1 = otherList.head;
            Node<T> curr2 = head;
            LinkedList<T> sortedList = new LinkedList<T>();
            while (curr1 != null || curr2 != null) {
                if (curr2 == null ||
                        (curr1 != null && curr1.getData().compareTo(curr2.getData()) < 0)) {
                  sortedList.addNode(curr1.getData());
                  curr1 = curr1.getNext();
                }
                else {
                  sortedList.addNode(curr2.getData());
                  curr2 = curr2.getNext();
                }
            }
            return  sortedList;
        }
    }
public LinkedList < T > sortedMergeList ( LinkedList otherList ) { if ( otherList == null ) { return this ; } else if ( head == null ) { return otherList ; } else { Node < T > curr1 = otherList . head ; Node < T > curr2 = head ; LinkedList < T > sortedList = new LinkedList < T > ( ) ; while ( curr1 != null || curr2 != null ) { if ( curr2 == null || ( curr1 != null && curr1 . getData ( ) . compareTo ( curr2 . getData ( ) ) < 0 ) ) { sortedList . addNode ( curr1 . getData ( ) ) ; curr1 = curr1 . getNext ( ) ; } else { sortedList . addNode ( curr2 . getData ( ) ) ; curr2 = curr2 . getNext ( ) ; } } return sortedList ; } }
Create a new sorted list which is the merged from two original sorted lists.
Create a new sorted list which is the merged from two original sorted lists .