/** * 删除倒数第几个节点 * * @return */ publicstatic Node removeNodeByDescPosition(Node a, int position){ if (a == null) { returnnull; } List<Node> list = new ArrayList<>();
Node head = new Node(null); head.next = a; Node tmp = head; while (tmp != null) { list.add(tmp); tmp = tmp.next; } //超过限制 if (position >= list.size()) { return head.next; }
//找到要删的节点的前置节点 Node n; int nodeidx = list.size() - position; n = list.get(nodeidx - 1); n.next = n.next.next; return head.next; }
publicstaticvoidmain(String[] args){ Node a = new Node(1); Node b = new Node(2); Node c = new Node(3); Node d = new Node(4); Node e = new Node(5); Node f = new Node(6); Node g = new Node(7); a.next = b; b.next = c; c.next = d; d.next = e; e.next = f; f.next = g;
// System.out.println(Node.findMid(a)); // // System.out.println(Node.checkNodeLoop(a)); // // // Node a1 = new Node(0); // Node b1 = new Node(2); // Node c1 = new Node(4); // Node d1 = new Node(6); // Node e1 = new Node(8); // Node f1 = new Node(10); // Node g1 = new Node(11); // a1.next = b1; // b1.next = c1; // c1.next = d1; // d1.next = e1; // e1.next = f1; // f1.next = g1; // // System.out.println(Node.merge(a, a1));