JAVA OJ练习(一)——设计链表

OJ链接:707. 设计链表

题目要求:在链表类中实现这些功能:

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

代码如下:

class MyLinkedList {
    
    Node head;
    int length;

    /** Initialize your data structure here. */
    public MyLinkedList() {
        length = 0;
        head = new Node(-1);
    }
    
    /** 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 >= length || index < 0)
            return -1;
        Node current = head;
        for(int i = 0; i <= index; i++)
            current = current.next;
        return current.value;
    }
    
    /** 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) {
        Node temp = new Node(val);
        temp.next = head.next;
        head.next = temp;
        length ++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        Node current = head;
        while(current.next != null)
            current = current.next;
        Node temp = new Node(val);
        current.next = temp;
        length ++;
    }
    
    /** 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 > length)
            return;
        if(index == length){
            addAtTail(val);
            return;
        }
        if(index < 0)
            index = index+length+1;
        Node current = head;
        for(int i = 0; i < index; i++)
            current = current.next;
        Node ptr = current.next;
        Node temp = new Node(val);
        current.next = temp;
        temp.next = ptr;
        length ++;
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if(index >= length || index < 0)
            return;
        Node current = head;
        for(int i = 0; i < index; i++)
            current = current.next;
        current.next = current.next.next;
        length --;
    }
    
    public void print(){
        Node current = head;
        StringBuilder builder = new StringBuilder();
        while(current != null){
            builder.append(String.valueOf(current.value) + " ");
            current = current.next;
        }
        System.out.println(builder);
    }
}

class Node{
    int value;
    Node next;
    
    public Node(int value){
        this.value = value;
    }
}
发布了43 篇原创文章 · 获赞 41 · 访问量 1781

猜你喜欢

转载自blog.csdn.net/weixin_45662626/article/details/103464977