首页 > 代码库 > 链表删除节点 欢迎喷 随便喷
链表删除节点 欢迎喷 随便喷
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; } }}
链表删除节点 欢迎喷 随便喷
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。