链表-双链表的设计

一、问题

1、index的范围究竟从1开始还是从0开始?

  /** 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;
	// 余下代码省略

在力扣参考答案中有这样的注释——如果index为负数,那么节点将添加在列表开头,index设置为0,也就是说,在第0个节点之前添加节点的函数调用是addAtIndex(0,val);
在这个函数的开头注释说明中写道,将一个节点添加到第index-th节点之前,也就是说index是从0开始的,index为0是有效的,我们应该说是第0个节点
size和index不同,size是从1开始的;

二、代码部分(自己只写了添加指定位置的值的部分)

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 equals to the length of linked list, the node will be appended to the end of linked list.
        // 根据这句话:在(index-1)th的节点后加节点
        if(index < 0 || index > size)return;

//        ListNode cur = head;
//        ListNode newNode = new ListNode(val);
//        for (int i = 0; i < size; i++) {
//            cur = cur.next;
//        }
//        cur.next = newNode;
//        newNode.prev = cur;
        ListNode newNode = new ListNode(val);
        ListNode pred, succ;
        if(index * 2 < size){
            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 - 1; i++) {
                succ = succ.prev;
            }
            pred = succ.prev;
        }
        pred.next = newNode;
        newNode.prev = pred;
        newNode.next = succ;
        succ.prev = newNode;
        size++;
    }

//    /** 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;
    }
}

class test{
    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addAtHead(0);
        list.addAtHead(1);
        list.addAtHead(2);
        list.addAtIndex(0,3);
        for (int i = 0; i < list.size; i++) {
            System.out.println(list.get(i));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值