[链表]leetcode707-设计链表

[链表]–设计链表


题目链接

leetcode 707.设计链表

题目

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

示例

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
linkedList.get(1);            //返回2
linkedList.deleteAtIndex(1);  //现在链表是1-> 3
linkedList.get(1);            //返回3

解析

单链表的实现:举个栗子画画图就行

代码实现

public class Solution707 {

    /**
     * Definition for singly-linked list
     */
    class ListNode {
        int val;
        ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    class MyLinkedList {
        ListNode head;
        int size;
        /** Initialize your data structure here. */
        public MyLinkedList() {
            this.size = 0;
            this.head = new ListNode(0);
        }

        /** 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 < 0 || index >= size) {
                return -1;
            }
            ListNode cur = this.head;
            for (int i = 0; i <= index; i++) {
                cur = cur.next;
            }
            return cur.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) {
            this.addAtIndex(0, val);
        }

        /** Append a node of value val to the last element of the linked list. */
        public void addAtTail(int val) {
            this.addAtIndex(size, val);
        }

        /** 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 > this.size) return;
            if (index < 0) index = 0;
            size++;
            ListNode prev = this.head;
            ListNode node = new ListNode(val);
            for (int i = 0; i < index; i++) {
                prev = prev.next;
            }
            node.next = prev.next;
            prev.next = node;
        }

        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= this.size) return;
            ListNode cur = this.head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            cur.next = cur.next.next;
            this.size--;
        }
    }

}

双链表的实现:相比于单链表操作时间快了很多但是也更复杂。实现一下带虚拟头尾的链表。

代码实现

public class Solution23 {
    /**
     * Definition for doubly-linked list
     */
    class ListNode {
        int val;
        ListNode prev;
        ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }

    class MyLinkedList {
        ListNode head;// 虚拟头节点
        ListNode tail;// 虚拟尾结点
        int size;
        /** Initialize your data structure here. */
        public MyLinkedList() {
            this.size = 0;
            this.head = new ListNode(0);
            this.tail = new ListNode(0);
            this.head.next = this.tail;
            this.tail.prev = this.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 < 0 || index >= this.size) return -1;
            ListNode cur = this.head;
            // 头部开始还是尾部开始
            if (index + 1 < this.size - index){
                for (int i = 0; i < index+1; i++) {
                    cur = cur.next;
                }
            }else {
                cur = this.tail;
                for (int i = 0; i < size-index; i++) {
                    cur = cur.prev;
                }
            }
            return cur.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 prev = this.head;
            ListNode next = prev.next;
            ListNode node = new ListNode(val);
            prev.next = node;
            node.next = next;
            next.prev = node;
            node.prev = prev;
            this.size++;
        }

        /** Append a node of value val to the last element of the linked list. */
        public void addAtTail(int val) {
            ListNode next = this.tail;
            ListNode prev = next.prev;
            ListNode node = new ListNode(val);
            prev.next = node;
            node.next = next;
            next.prev = node;
            node.prev = prev;
            this.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 > this.size) return;
            if (index < 0) index = 0;
            ListNode prev,next;// 插入位置的前驱后继
            // 判断头部遍历还是尾部遍历
            if (index < this.size - index) {
                prev = this.head;
                for (int i = 0; i < index; i++) {
                    prev = prev.next;
                }
                next = prev.next;
            }else {
                next = this.tail;
                for (int i = 0; i < size-index; i++) {
                    next = next.prev;
                }
                prev = next.prev;
            }
            size++;
            ListNode node = new ListNode(val);
            prev.next = node;
            node.next = next;
            next.prev = node;
            node.prev = prev;
        }

        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= this.size) return;
            // 判断头部遍历还是尾部遍历
            ListNode prev, next;
            if (index < this.size - index) {
                prev = this.head;
                for (int i = 0; i < index; i++) {
                    prev = prev.next;
                }
                next = prev.next.next;
            }else {
                next = this.tail;
                for (int i = 0; i < size-index-1; i++) {
                    next = next.prev;
                }
                prev = next.prev.prev;
            }
            prev.next = next;
            next.prev = prev;
            this.size--;
        }
    }

-----------------------------------------------------------------------------有始有终分割线----------------------------------------------------------------------------------

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值