public class MyListNode { private static Node head = null;//头节点 //链表的每个节点类 private class Node{ int data;//每个节点的数据 Node next;//每个节点指向下一个节点的连接 public Node(int data){ this.data = data; } } public int length(){ int size = 0; Node tmp = head; while(tmp!=null){ size++; tmp = tmp.next; } return size; } //从尾部插入节点 public void addNode(int d){ Node newNode = new Node(d); if(head == null){ head = newNode; return; } Node tmp = head; while(tmp.next!=null){ tmp = tmp.next; } //add new node in the end tmp.next = newNode; } //从头部插入节点 public void addHead(int s){ Node newhead = new Node(s); if(head == null){ head = newhead; } else{ newhead.next = head; head = newhead; } } //删除节点 public void delNode(int a){ if(a<1||a>length()){ System.out.println("false"); } else if(a==1){ head = head.next; System.out.println("true"); } else{ int i=1; Node prenode = head; Node curnode = prenode.next; while(curnode!=null){ if(i==(a-1)){ prenode.next = curnode.next; } prenode = curnode; curnode = curnode.next; i++; } System.out.println("true"); } } //删除未知头节点的节点 public void delnodenoh(Node delnode){ if(delnode==null||delnode.next==null){ System.out.println("This node can not be delected."); } int tmp = delnode.data; delnode.data = delnode.next.data; delnode.next.data = tmp; delnode.next = delnode.next.next; System.out.println("This node is delected."); } //删除重复节点 public void delrepeatNode(Node head){ Node p=head; while(p!=null){ Node q=p; while(q.next!=null){ if(p.data==q.next.data){ q.next = q.next.next; } else{ q = q.next; } } p = p.next; } } //查找节点 public Node findnode(Node head, int k){ if(k<1||k>this.length()){ return null; } Node p1 = head; Node p2 = head; for(int i=0;i<k-1;i++) p1 = p1.next; while(p1!=null){ p1 = p1.next; p2 = p2.next; } return p2; } //查找中间节点 public Node findcenter(Node head){ Node p1 = head; Node p2 = head; while(p1!=null&&p1.next!=null&&p1.next.next!=null){ p1 = p1.next.next; p2 = p2.next; } return p2; } //判断是否有环 public void Loop(Node head){ Node fast = head; Node slow = head; if(fast==null){ System.out.println("false"); } while(fast!=null&&fast.next!=null){ fast = fast.next.next; slow = slow.next; if(fast==slow){ System.out.println("true"); } } System.out.println("false"); } //查找环交点 public Node Loopport(Node head){ Node fast = head; Node slow = head; while(fast!=null&&fast.next!=null){ fast = fast.next.next; slow = slow.next; if(fast==slow) break; } if(fast==null||fast.next==null) return null; slow = head; while(slow!=fast){ slow = slow.next; fast = fast.next; } return slow; } //是否相交 public Boolean meet(Node p1, Node p2){ if(p1==null||p2==null){ return false; } Node tail1 = p1; while(tail1.next!=null) tail1 = tail1.next; Node tail2 = p2; while(tail2.next!=null) tail2 = tail2.next; return tail1==tail2; } //相交节点 public Node meetnode(Node p1, Node p2){ if(p1==null||p2==null){ return null; } Node tail1 = p1; int len1 = 0; while(tail1.next!=null){ tail1 = p1.next; len1++; } Node tail2 = p2; int len2 = 0; while(tail2.next!=null){ tail2 = tail2.next; len2++; } if(tail1!=tail2) return null; Node t1 = p1; Node t2 = p2; if(len1>len2){ int d = len1-len2; while(d!=0){ t1 = t1.next; d--; } } else{ int d = len2-len1; while(d!=0){ t2 = t2.next; d--; } } while(t1!=t2){ t1 = t1.next; t2 = t2.next; } return t1; } //反转链表 public Node reverseListNode(Node head){ //单链表为空或只有一个节点,直接返回原单链表 if (head == null || head.next == null){ return head; } Node preNode = null; Node curNode = head; while (curNode != null){ Node tmp = curNode.next; curNode.next = preNode; preNode = curNode; curNode = tmp; } return this.head = preNode; } //链表排序 public Node orderNode(){ Node curNode = head; while(curNode != null){ Node nextNode = curNode.next; while(nextNode != null){ if(curNode.data > nextNode.data){ int temp = curNode.data; curNode.data = nextNode.data; nextNode.data = temp; } nextNode = nextNode.next; } curNode = curNode.next; } return head; } //输出链表 public void printList(){ Node tmp = head; while(tmp!=null){ System.out.print(tmp.data+" "); tmp = tmp.next; } System.out.println(); } //输出反转链表 public void printListReversely(Node phead){ if(phead!=null){ printListReversely(phead.next); System.out.print(phead.data+" "); } } public static void main(String[] args){ MyListNode list = new MyListNode(); list.addHead(5); list.addHead(4); list.addHead(7); list.addHead(2); list.addHead(6); list.addHead(4); System.out.println("length = "+list.length()); list.printList(); list.addNode(5); list.addNode(4); list.addNode(7); list.addNode(2); list.addNode(6); list.addNode(4); System.out.println("length = "+list.length()); System.out.println("before order:"); list.printList(); // list.findnode(head, 1); list.orderNode(); System.out.println("after order:"); list.printList(); list.delNode(4); list.printList(); list.reverseListNode(head); list.printList(); list.delrepeatNode(head); list.printList(); list.printListReversely(head); } }
原文:https://www.cnblogs.com/jocelynD-9/p/11262747.html