LeetCode【707.设计链表】

版权声明: https://blog.csdn.net/qq_38386316/article/details/83030107

题目描述

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


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

  • get(index) :获取链表中的第index个节点的值。如果索引无效,则返回-1
  • addAtHead(val): 在链表的第一个元素之前添加一个值为val的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val): 将值为val的节点追加到链表的最后一个元素。
  • addAtIndex(index, val): 在链表中的第index个节点之前添加值为val的节点。如果index等于链表的长度,则该节点将附加到链表的末尾。如果index大于;链表长度,则不会插入节点。
  • 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

思路
此题包含了链表的创建、插入、删除、查找,是链表中比较有综合性的问题。查找和删除时,注意一下索引的序号。

代码

class ListNode {
    int val;
    ListNode next;
    ListNode (int val) {this.val = val;}
}
class MyLinkedList {

    /** Initialize your data structure here. */
    ListNode head = null;
    public MyLinkedList() {
         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) {
        ListNode cur = head.next;
        int num = -1;
        while(cur != null) {
            num ++;
            if(num == index) {
                return cur.val;
            }  
            cur = cur.next;
        }
        return -1;
    }
    
    /** 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 node = new ListNode(val);
        node.next = head.next;
        head.next = node;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = new ListNode(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) {
        ListNode cur = head;
        int num = -1;
        while(cur != null) {
            num ++;
            if(num == index ) {
                ListNode node = new ListNode(val);
                node.next = cur.next;
                cur.next = node;
            }
            cur = cur.next;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        ListNode cur = head;
        int num = -1;
        while(cur.next != null) {
            num ++;
            if(num == index ) {
                ListNode temp = cur.next;
                cur.next = temp.next;
                break;
            }
            cur = cur.next;
        }
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

猜你喜欢

转载自blog.csdn.net/qq_38386316/article/details/83030107