首页 > 代码库 > 链表删除节点 欢迎喷 随便喷

链表删除节点 欢迎喷 随便喷

public class Node {    double data = http://www.mamicode.com/0;    Node next = null;    public Node(double data) {        this.data =http://www.mamicode.com/ data;    }    public static Node reverse(Node head) {        Node a = head;        Node b = a.next;        a.next = null;        Node tmp = null;        while (b != null) {            tmp = b.next;            b.next = a;            a = b;            b = tmp;        }        return a;    }    public static Node del(Node root, int k) {        if (root == null) {            return root;        }        if (k <= 0) {            return root;        }        if (root.next == null && k == 1) {            root = null;            return root;        }        if (root.next == null && k != 1) {            return root;        }        if(k==1){            root=root.next;            return root;        }        int num = 1;        Node tmp = root;        Node p = tmp.next;        num++;        while (p != null) {            if (k == num) {                Node q = p.next;                if (q != null) {                    tmp.next = q;                    return root;                } else {                    tmp.next = null;                }            }            tmp = p;            p = p.next;            num++;        }        return root;    }    public static void main(String[] args) {        Node head = new Node(11);        Node temp = head;        for (int i = 0; i < 8; i++) {            Node p = new Node(i);            temp.next = p;            temp = p;        }        head=del(head, 10);        while(head!=null){            System.out.println(head.data);            head=head.next;        }                }}

 

链表删除节点 欢迎喷 随便喷