Insert a integer as a ListNode to a sorted linked list.
public ListNode insert(ListNode head, int num) {
if (head == null) {
return new ListNode(num);
}
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
ListNode node = new ListNode(num);
while (head.next != null) {
if (num > head.next.val) {
head = head.next;
} else {
node.next = head.next;
head.next = node;
break;
}
}
if (head.next == null) {
head.next = node;
}
return dummy.next;
}