LeetCode刷题记录

LeetCode 707. 设计链表

2019-06-30  本文已影响0人  TheKey_

707. 设计链表

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

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


思路:
1.通过维护一个虚拟的头节点dummyHead来操作链表,从而实现添加和删除操作。

public class MyLinkedList {

    private class Node {
        public int val;
        public Node next;

        public Node(int val, Node next) {
            this.val = val;
            this.next = next;
        }

        public Node(int val) {
            this(val, null);
        }

        public Node() {
            this(0, null);
        }
    }

    //使用虚拟头节点
    private Node dummyHead;
    private int size;

    /**
     * Initialize your data structure here.
     */
    public MyLinkedList() {
        dummyHead = new Node();
        size = 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;
        Node cur = dummyHead.next;   //头节点
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.val;
    }

    /**
     * judge linked list is empty?
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 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) {
        addAtIndex(0, val);
    }

    /**
     * Append a node of value val to the last element of the linked list.
     */
    public void addAtTail(int val) {
        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 < 0 || index > size) {
            return;
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(val, prev.next);
        size++;
    }

    /**
     * Delete the index-th node in the linked list, if the index is valid.
     */
    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("Add filed, index is illegal");
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;     //loitering object != memory leak
        size--;
    }
}

思路:
通过维护当前节点cur完成相关操作

public class MyLinkedList2 {

    private class Node {

        private int val;
        private Node next;

        public Node(int val, Node next) {
            this.val = val;
            this.next = next;
        }

        public Node(int val) {
            this(val, null);
        }

        public Node() {
            this(0, null);
        }
    }

    private Node head;
    private int size;

    public MyLinkedList2() {
        head = null;
        size = 0;
    }

    public void addAtHead(int val) {
//        Node node = new Node(val);
//        node.next = head;
//        head = node;
        head = new Node(val, head);
        size ++;
    }

    public void addAtIndex(int index, int val) {
        if (index < 0 || index > size) {
            return;
        }
        if (index == 0) {
            addAtHead(val);
        } else {
            Node prev = head;
            for (int i = 0; i < index - 1; i++) {
                prev = prev.next;
            }
            prev.next = new Node(val, prev.next);
            size ++;
        }

    }

    public void deleteAtHead() {
        head = head.next;
        size--;
    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) {
            return;
        }
        if (index == 0) {
            deleteAtHead();
        } else {
            Node prev = head;
            for (int i = 0; i < index - 1; i++) {
                prev = prev.next;
            }
            Node cur = prev.next;
            prev.next = cur.next;
            cur.next = null;
            size--;
        }
    }

    public void addAtTail(int val) {
        addAtIndex(size, val);
    }

    public int get(int index) {
        if (index < 0 || index >= size) {
            return -1;
        }
        if (index == 0) {
            return head.val;
        } else {
            Node cur = head.next;
            for (int i = 0; i < index - 1; i++) {
                cur = cur.next;
            }
            return cur.val;
        }
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = head;
        while (cur != null) {
            res.append(cur.val + "->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }
 }   
public static void main(String[] args) {
        MyLinkedList2 myLinkedList2 = new MyLinkedList2();
        myLinkedList2.addAtHead(1);
        System.out.println(myLinkedList2);
        myLinkedList2.addAtTail(3);
        System.out.println(myLinkedList2);
        myLinkedList2.addAtIndex(1, 2);
        System.out.println(myLinkedList2);
        myLinkedList2.deleteAtIndex(1);
        System.out.println(myLinkedList2);
        myLinkedList2.get(1);
        System.out.println(myLinkedList2);
    }
不知道测试用例是否有问题,一直无法通过leetcode中最后一个用例。

上一篇下一篇

猜你喜欢

热点阅读