LeetCode 707 design linked list -- realize the basic operation of linked list

Posted by apaxson on Mon, 10 Jan 2022 16:05:46 +0100

Source: LeetCode
Link: https://leetcode-cn.com/problems/design-linked-list

Design the implementation of linked list. You can choose to use single linked list or double linked list. A node in a single linked list should have two attributes: val , and val , next. Val , is the value of the current node, and next , is the pointer / reference to the next node. If you want to use a two-way linked list, you also need an attribute prev to indicate the previous node in the linked list. It is assumed that all nodes in the linked list are 0-index.

Implement these functions in the linked list class:

get(index): get the value of the {index} node in the linked list. Returns - 1 if the index is invalid.
addAtHead(val): add a node with a value of val before the first element of the linked list. After insertion, the new node will become the first node in the linked list.
addAtTail(val): append the node with the value of val to the last element of the linked list.
addAtIndex(index,val): add a node with a value of val before the , index , node in the linked list. If the index is equal to the length of the linked list, the node will be attached to the end of the linked list. If the index is greater than the length of the linked list, the node will not be inserted. If the index is less than 0, the node is inserted in the header.
deleteAtIndex(index): if the index index is valid, delete the {index node in the linked list.
 

Example:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); / / the linked list becomes 1 - > 2 - > 3
linkedList.get(1); / / return 2
linkedList.deleteAtIndex(1); / / now the linked list is 1 - > 3
linkedList.get(1); / / return 3
 

Tips:

All val values are within [1, 1000].
The number of operations will be within [1, 1000].
Do not use the built-in LinkedList library.

Reference article: A topic examines the five common operations of the linked list! ​

Idea:

This topic is mainly to realize some common basic operations of the linked list. You need to understand the basic concept of the linked list and the implementation principle of the operation. Here I use the virtual head node. It should be noted that I didn't expect to declare the ListNode class in the class MyLinkedList. At first, I thought it was a bit messy to directly use the MyLinkedList class as the node in the linked list. After reading the article, I didn't expect that it was written as described above, which is much simpler to write.

Java code:

class MyLinkedList {

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

    private ListNode head;
    private int size;

    public MyLinkedList() {
        head = new ListNode(0, null);
        size = 0;
    }
    
    public int get(int index) {
        if(size <= index)return -1;
        ListNode rnNode = head.next;
        while(index-- != 0){
            rnNode = rnNode.next;
        }
        return rnNode.val;
    }
    
    public void addAtHead(int val) {
        ListNode newNode = new ListNode(val, head.next);
        head.next = newNode;
        size++;
        //print();
    }
    
    public void addAtTail(int val) {
        ListNode rnNode = head;
        ListNode newNode = new ListNode(val,null);
        while(rnNode.next != null){
            rnNode = rnNode.next;
        }
        rnNode.next = newNode;
        size++;
        //print();
    }
    
    public void addAtIndex(int index, int val) {
        if(size < index)return;
        else if(index < 0)addAtHead(val);
        ListNode rnNode = head;
        while(index-- != 0){
            rnNode = rnNode.next;
        }
        ListNode newNode = new ListNode(val, rnNode.next);
        rnNode.next = newNode;
        size++;
        //print();
    }
    
    public void deleteAtIndex(int index) {
        if(size <= index)return;
        ListNode rnNode = head;
        while(index-- != 0){
            rnNode = rnNode.next;
        }
        rnNode.next = rnNode.next.next;
        size--;
        //print();
    }

    //Used to print linked lists
    private void print(){
        ListNode rnNode = head.next;
        while(rnNode != null){
            System.out.print(rnNode.val+" ");
            rnNode = rnNode.next;
        }
        System.out.println();
    }
}

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

Topics: data structure leetcode linked list