13 在O(1)时间内删除链表节点

来源:互联网 发布:大数据与BI 编辑:程序博客网 时间:2024/06/13 06:10

描述:





实现代码:

import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.LinkedList;import java.util.Scanner;class Node{int val;Node next;public Node(int val) {// TODO Auto-generated constructor stubthis.val=val;}}public class MyLinkedList1 {private static Node head,tail;   // 链表的 首节点和尾节点public MyLinkedList1() {// TODO Auto-generated constructor stubhead=tail=null;}public static boolean isEmpty() {return head==null;}// 头插法public static void addFirst(int data){Node newNode=new Node(data);newNode.next=head;head=newNode;}// 添加一组数据public static void add (Collection<Integer> c){Object[] a = c.toArray();int numNew = a.length;if (numNew == 0)return ;for (Object o : a) {addTail((int) o);}}// 尾插法public static void addTail(int data){Node newNode=new Node(data);if (tail==null) {head=tail=newNode;}else {tail.next=newNode;tail=newNode;}}// 删除节点,本题的考查函数public static voiddeleteNode(Node nodeDel){if (head==null||nodeDel==null) {return;}// 要删除的节点不是尾节点if (tail!=nodeDel) {Node next=nodeDel.next;nodeDel.val=next.val;nodeDel.next=next.next;}if (tail==head) {nodeDel=tail=null;}if (tail==nodeDel) {Node current=head;while (current.next!=tail) {current=current.next;}current.next=nodeDel=null;tail=current;}}public static void main(String[] args) {ArrayList<Integer> arrayList=new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));add(arrayList);Node nodeDel=getNode(3);deleteNode(nodeDel);display (head);}// 打印链表private static void display(Node head) {// TODO Auto-generated method stubif (head==null) {return;}System.out.println(head.val);display(head.next);}// 得到 下标为 index 的结点private static Node getNode(int index) {// TODO Auto-generated method stubif (index==0) {return head;}Node cur=head;for (int i = 1; i <=index; i++) {cur=cur.next;}return cur;}}

测试结果:



0 0