Пример #1
0
  /**
   * Returns the value of the node in the specified column (position) of the list
   *
   * @param col The column of the node whose value is being returned
   * @return The value of the node
   */
  public int nodeValue(int col) {

    ListNode currentNode = first;
    int index = 0;

    while (currentNode != null) {

      if (col == index) {
        return currentNode.value();
      } else currentNode = currentNode.next;
      index++;
    }

    // if this point is reached then we have asked for a node that is not in the scope of the list
    return -1;
  }
Пример #2
0
 public static void deleteNode(ListNode head, ListNode aim) {
   if (head == null | aim == null) return;
   if (head.next == aim) { // length = 1
     head.next = null;
   } else { // length>1
     if (aim.next == null) { // aim为尾节点
       ListNode current = head;
       while (current.next != aim) {
         current = current.next;
       }
       current.next = aim.next;
       aim.next = null;
     } else { // aim不为尾节点
       aim.value = aim.next.value;
       aim.next = aim.next.next;
       aim.next.next = null;
     }
   }
 }