链表删除节点

賈小強
转载请注明原创出处,谢谢!

package com.lab2.test2;

public class DeleteNode {
    public static void main(String[] args) {
        Node node1 = new Node();
        node1.data = 1;
        Node node2 = new Node();
        node2.data = 2;
        Node node3 = new Node();
        node3.data = 3;

        node1.next = node2;
        node2.next = node3;

        delNode(node2);

        Node current = node1;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    private static void delNode(Node node) {
        Node next = node.next;
        node.data = next.data;
        node.next = next.next;
    }

}

输出

1 3 

上面的代码通过将后继节点的数据复制到当前节点,从而删除当前节点的方式完成了操作
注意:如果直接用下面的方式并不会改变链表(删除node2,需要让node1的next指向node3,下面的方法并没有完成这个操作,只是node2变量指向了node2.next,但是node2节点还是被node1.next指向)

package com.lab2.test2;

public class DeleteNode {
    public static void main(String[] args) {
        Node node1 = new Node();
        node1.data = 1;
        Node node2 = new Node();
        node2.data = 2;
        Node node3 = new Node();
        node3.data = 3;

        node1.next = node2;
        node2.next = node3;

        node2=node2.next;

        Node current = node1;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    private static void delNode(Node node) {
        node=node.next;
    }

}

输出

1 2 3

不过如果是删除首节点则可以,比如下面LinkedStack中的pop方法

package com.lab1.test1;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedStack implements Iterable {
    private int n;
    private Node first;

    private class Node {
        private Item item;
        private Node next;
    }

    @Override
    public Iterator iterator() {
        return new ListIterator();
    }

    private class ListIterator implements Iterator {
        Node current = first;

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }

    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (Item item : this) {
            builder.append(item + " ");
        }
        return builder.toString();
    }

    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return n;
    }

    public void push(Item item) {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        n++;
    }

    public Item pop() {
        if (isEmpty()) {
            throw new NoSuchElementException("empty stack exception");
        }
        Item item = first.item;
        first = first.next;
        n--;
        return item;
    }

    public static void main(String[] args) {
        LinkedStack stack = new LinkedStack<>();
        System.out.println(stack);
        System.out.println(stack.size());
        System.out.println(stack.isEmpty());

        stack.push("bill");
        stack.push("jack");
        stack.push("lucy");
        System.out.println(stack);
        System.out.println(stack.size());
        System.out.println(stack.isEmpty());

        stack.pop();
        stack.pop();
        System.out.println(stack);
        System.out.println(stack.size());
        System.out.println(stack.isEmpty());
    }

}

Happy learning !!

你可能感兴趣的:(链表删除节点)