zoukankan      html  css  js  c++  java
  • 双链表

    通过比较 index 和 size - index 的大小判断从头开始较快还是从尾巴开始较快

    public class ListNode {
      int val;
      ListNode next;
      ListNode prev;
      ListNode(int x) { val = x; } //这个只是用来初始化 隐藏的节点的
    }
    
    class MyLinkedList {
      int size;
      // sentinel nodes as pseudo-head and pseudo-tail
      ListNode head, tail;
      public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
        tail = new ListNode(0);
        head.next = tail;
        tail.prev = head;
      }
    
      /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
      public int get(int index) {
        // if index is invalid
        if (index < 0 || index >= size) return -1;
    
        // choose the fastest way: to move from the head
        // or to move from the tail
        ListNode curr = head;
        if (index + 1 < size - index)
          for(int i = 0; i < index + 1; ++i) curr = curr.next;
        else {
          curr = tail;
          for(int i = 0; i < size - index; ++i) curr = curr.prev;
        }
    
        return curr.val;
      }
    
      /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
      public void addAtHead(int val) {
        ListNode pred = head, succ = head.next;
    
        ++size;
        ListNode toAdd = new ListNode(val);
        toAdd.prev = pred;
        toAdd.next = succ;
        pred.next = toAdd;
        succ.prev = toAdd;
      }
    
      /** Append a node of value val to the last element of the linked list. */
      public void addAtTail(int val) {
        ListNode succ = tail, pred = tail.prev;
    
        ++size;
        ListNode toAdd = new ListNode(val);
        toAdd.prev = pred;
        toAdd.next = succ;
        pred.next = toAdd;
        succ.prev = toAdd;
      }
    
      /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
      public void addAtIndex(int index, int val) {
        // If index is greater than the length, 
        // the node will not be inserted.
        if (index > size) return;
    
        // [so weird] If index is negative, 
        // the node will be inserted at the head of the list.
        if (index < 0) index = 0;
    
        // find predecessor and successor of the node to be added
        ListNode pred, succ;
        if (index < size - index) {
          pred = head;
          for(int i = 0; i < index; ++i) pred = pred.next;
          succ = pred.next;
        }
        else {
          succ = tail;
          for (int i = 0; i < size - index; ++i) succ = succ.prev;
          pred = succ.prev;
        }
    
        // insertion itself
        ++size;
        ListNode toAdd = new ListNode(val);
        toAdd.prev = pred;
        toAdd.next = succ;
        pred.next = toAdd;
        succ.prev = toAdd;
      }
    
      /** Delete the index-th node in the linked list, if the index is valid. */
      public void deleteAtIndex(int index) {
        // if the index is invalid, do nothing
        if (index < 0 || index >= size) return;
    
        // find predecessor and successor of the node to be deleted
        ListNode pred, succ;
        if (index < size - index) {
          pred = head;
          for(int i = 0; i < index; ++i) pred = pred.next;
          succ = pred.next.next;
        }
        else {
          succ = tail;
          for (int i = 0; i < size - index - 1; ++i) succ = succ.prev;
          pred = succ.prev.prev;
        }
    
        // delete pred.next 
        --size;
        pred.next = succ;
        succ.prev = pred;
      }
    }

     

  • 相关阅读:
    05-Selenium的三种等待
    04-selenium 八大元素定位方法
    03-Selenium简单操作
    python-利用json模块处理json数据几个函数总结
    python-利用pymysql获取数据简单使用总结
    python-利用faker模块生成测试数据
    python-利用random模块生成测试数据封装方法总结
    python-利用shutil模块rmtree方法可以将文件及其文件夹下的内容删除
    pytest--配置用例执行顺序(pytest_ordering插件介绍)
    pytest--mark基本使用(主要通过pytest.ini文件注册标签名,对用例进行标记分组)
  • 原文地址:https://www.cnblogs.com/focusonoutput/p/13501468.html
Copyright © 2011-2022 走看看