码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-28 22:28:06      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   div   log   sp   amp   

public class Node {
    double data = 0;
    Node next = null;

    public Node(double data) {
        this.data = 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;
        }
            
    }
}

 

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

标签:style   blog   color   ar   for   div   log   sp   amp   

原文地址:http://www.cnblogs.com/cclz/p/3942410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!