1 public class Solution {
 2   public ListNode insert(ListNode head, int value) {
 3     // Write your solution here
 4       if (head == null) {
 5          return new ListNode(value); 
 6     }
 7     if (head.value >= value) {
 8          ListNode newHead = new ListNode(value);
 9       newHead.next = head;
10       return newHead;
11     }
12     ListNode cur = head;
13     while (cur.next != null && cur.value <= value) {
14       if (cur.next.value >= value) {
15         ListNode node = new ListNode(value);
16         node.next = cur.next;
17         cur.next = node;
18         break;
19       }
20          cur = cur.next; 
21     }
22     
23     if (cur.next == null) {
24          ListNode node = new ListNode(value);
25          cur.next = node;
26       node.next = null;
27     }
28     return head;
29   }
30 }

 

posted on 2018-03-02 07:59  mayinmiao  阅读(82)  评论(0编辑  收藏  举报