algorithm - 按引用或按值链接列表?

标签 algorithm data-structures linked-list

问题来了。假设链表实现如下(Java):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

考虑链表:

1 -> 2 -> 3 -> 4

我做了类似的事情:

ListNode newHead = head;
newHead = head.next.next; 
//Now newHead is pointing to (3) in the linked list.

现在我施展魔法:

newHead.val = 87

链表变为:

1 -> 2 -> 87 -> 4

如果我打印了 headNOT newHead

这是为什么?我没有用 head 修改任何东西,它仍然改变了?

最佳答案

所以你可以使用这个:

节点类:

public class IntNode {

    int value;
    IntNode next;

    public IntNode(int value) {
        this.value = value;
    }
}

单链表类:

/**
 * A singly-linked list of integer values with fast addFirst and addLast methods
 */
public class LinkedIntList {

    IntNode first;
    IntNode last;
    int size;

    /**
     * Return the integer value at position 'index'
     */
    int get(int index) {
        return getNode(index).value;
    }

    /**
     * Set the integer value at position 'index' to 'value'
     */
    void set(int index, int value) {
        getNode(index).value = value;
    }

    /**
     * Returns whether the list is empty (has no values)
     */
    boolean isEmpty() {
        return size == 0;
    }

    /**
     * Inserts 'value' at position 0 in the list.
     */
    void addFirst(int value) {
        IntNode newNode = new IntNode(value);
        newNode.next = first;
        first = newNode;
        if(last == null)
            last = newNode;
        size++;
    }

    /**
     * Appends 'value' at the end of the list.
     */
    void addLast(int value) {
        IntNode newNode = new IntNode(value);
        if(isEmpty())
            first = newNode;
        else
            last.next = newNode;

        last = newNode;
        size++;
    }

    /**
     * Removes and returns the first value of the list.
     */
    int removeFirst() {
        if(isEmpty()) {
            System.out.println("RemoveFirst() on empty list!");
            System.exit(-1);
        }

        int value = first.value;
        if(first == last) {
            // List has only one element, so just clear it
            clear();
        }
        else {
            first = first.next;
            size--;
        }

        return value;
    }

    /**
     * Removes and returns the last value of the list.
     */
    int removeLast() {
        if(isEmpty()) {
            System.out.println("RemoveLast() on empty list!");
            System.exit(-1);
        }

        int value = last.value;
        if(first == last) {
            // List has only one element, so just clear it
            clear();
        }
        else {
            // List has more than one element
            IntNode currentNode = first;
            while(currentNode.next != last)
                currentNode = currentNode.next;

            currentNode.next = null;
            last = currentNode;
            size--;
        }
        return value;
    }

    /**
     * Removes all values from the list, making the list empty.
     */
    void clear() {
        first = last = null;
        size = 0;
    }

    /**
     * Returns a new int-array with the same contents as the list.
     */
    int[] toArray() {
        int[] array = new int[size];
        int i = 0;
        for(IntNode n = first; n != null; n = n.next, i++)
            array[i] = n.value;
        return array;
    }

    /**
     * For internal use only.
     */
    IntNode getNode(int index) {
        if(index < 0 || index >= size) {
            System.out.println("GetNode() with invalid index: " + index);
            System.exit(-1);
        }

        IntNode current = first;
        for(int i = 0; i < index; i++)
            current = current.next;
        return current;
    }
}

说明见代码注释

关于algorithm - 按引用或按值链接列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813968/

相关文章:

c# - 如何在 C# 中更快地计算简单移动平均线?

c# - 实现这个的最佳数据结构是什么? [C# 中的嵌套属性]

java - 我的数组中的链接列表不起作用

c++ - O(N) 中的锦标赛获胜者和 O(NLogN) 中的玩家排名

algorithm - 霍尔分区的正确性

c - 如何让链表的最后一个节点参与冒泡排序?

algorithm - B+树中的非叶节点

c - 函数头中struct *和struct **的区别

c - 为什么带有双指针的链表会导致错误?

Photoshop 中阴影/高光滤镜背后的算法