예제 #1
0
 public void removeNode(ListNode a, int val) {
   if (a.val == val) {
     // ListNode b = a.next;
     // a.val = b.val;
     // a.next = b.next;
     a.val = a.next.val;
     a.next = a.next.next;
   } else {
     while (a.next.val != val) {
       a = a.next;
     }
     a.next = a.next.next;
   }
 }
예제 #2
0
  public void insertNthNode(ListNode a, int n, int val) {

    if (n == 1) { // !!!!!!!!!!!!!!!!!!
      a = insertFrontNode(a, val);
    } else {
      ListNode b = new ListNode(val);
      int i = 1;
      while (a.next != null && i != n - 1) {
        i++;
        a = a.next;
      }
      b.next = a.next;
      a.next = b;
    }
  }
예제 #3
0
 public void removeNthNode(ListNode a, int n) {
   if (n == 1) {
     // ListNode b = a.next;
     // a.val = b.val;
     // a.next = b.next;
     a.val = a.next.val;
     a.next = a.next.next;
   } else {
     int i = 1;
     while (a.next != null && i != n - 1) {
       i++;
       a = a.next;
     }
     a.next = a.next.next;
   }
 }
예제 #4
0
 public static void addNode(ListNode a, int val) {
   ListNode b = new ListNode(val);
   while (a.next != null) {
     a = a.next;
   }
   a.next = b;
 }
예제 #5
0
 public void updateNthNode(ListNode a, int n, int val) {
   if (n == 1) {
     a.val = val;
   } else {
     int i = 1;
     while (a.next != null & i != n - 1) {
       i++;
       a = a.next;
     }
     a.next.val = val;
   }
 }
예제 #6
0
  public ListNode mergeKLists(List<ListNode> lists) {
    SmallRootHeap<ListNode2> heap = new SmallRootHeap<ListNode2>();
    for (ListNode node : lists) {
      if (node != null) heap.add(new ListNode2(node));
    }
    if (heap.empty()) return null;
    ListNode head = heap.pop(), ptr = head;
    if (ptr.next != null) {
      heap.add(new ListNode2(ptr.next));
      ptr.next = null;
    }
    while (!heap.empty()) {
      ptr.next = heap.pop();
      ptr = ptr.next;
      if (ptr.next != null) {
        heap.add(new ListNode2(ptr.next));
        ptr.next = null;
      }
    }

    return head;
  }
예제 #7
0
 public ListNode mergeKLists(ListNode[] lists) {
   int len = 0;
   for (int i = 0; i < lists.length; i++)
     if (lists[i] != null) {
       lists[len++] = lists[i];
     }
   buildHeap(lists, len);
   ListNode resHead = new ListNode(0);
   ListNode resRear = resHead;
   while (len > 1) {
     resRear.next = lists[0];
     resRear = lists[0];
     if (lists[0].next == null) {
       lists[0] = lists[len - 1];
       len--;
       sortHeap(lists, 0, len);
     } else {
       lists[0] = lists[0].next;
       sortHeap(lists, 0, len);
     }
   }
   resRear.next = lists[0];
   return resHead.next;
 }
 public static void main(String args[]) {
   ListNode a = new ListNode(1);
   ListNode b = new ListNode(2);
   ListNode c = new ListNode(3);
   ListNode d = new ListNode(4);
   ListNode e = new ListNode(5);
   a.next = b;
   b.next = c;
   c.next = d;
   d.next = e;
   TreeNode ans = sortedListToBST(a);
   System.out.println("root= " + ans.val);
   System.out.println("root.left= " + ans.left.val);
   System.out.println("root.right= " + ans.right.val);
   TreeNode one = ans.left;
   System.out.println(one.right.val);
 }
예제 #9
0
 public ListNode insertFrontNode(ListNode a, int val) {
   ListNode b = new ListNode(val);
   b.next = a;
   return b;
 }
예제 #10
0
 public void deleteNode(ListNode node) {
   if (node == null) return;
   node.val = node.next.val;
   node.next = node.next.next;
 }