链表的各种操作
public class Node {
int val;
Node next = null;
Node(int val) {
this.val = val;
}
}
package one;
public class MyList {
static Node head=null;
static Node tem = null;//建立一个临时变量
public void findElem(Node head,int k) {//输出链表倒数第K个值
if(k<1)
return;
Node p1 = head;
Node p2 = head;
for(int i=0;i<k-1&&p1!=null;i++) {
p1=p1.next;
}
if (p1==null) {
System.out.println("K不合法");
return;
}
while(p1.next!=null) {
p1=p1.next;
p2=p2.next;
}
System.out.println(+p2.data);;
}
public void addNode(int d) {//在链表中添加节点
Node newNode = new Node(d);
if(head==null) {
head = newNode;
return;
}
tem = head;
while(tem.next!=null) {
tem = tem.next;
}
tem.next = newNode;
}
public void outputNode() {//输出链表中的每一个值
Node i = head;
while(i!=null) {
System.out.println(+i.data);
i = i.next;
}
}
public void ReverseIteratively(Node head) {//实现链表的反转
Node pReversedHead = head;
Node pNode = head;
Node pPrev = null;
while(pNode != null) {
Node pNext = pNode.next;
if(pNext==null) {
pReversedHead=pNode;
}
pNode.next=pPrev;
pPrev=pNode;
pNode=pNext;
}
this.head = pReversedHead;
}
public void printListReversely(Node pListHead) {//从尾到头输出链表
if(pListHead!=null) {
printListReversely(pListHead.next);
System.out.println(pListHead.data);
}
}
public void SearchMid(Node head) {//寻找单链表的中间节点
//利用两个指针,一个快指针一次走两步,一个慢指针一次走1步
Node p =this.head;//this首先是一个对象,它代表调用这个函数的对象
Node q = this.head;
while(q.next!=null) {
p=p.next;
q=q.next.next;
}
System.out.println(+p.data);
}
public boolean IsLoop(Node head) {//判断一个链表中是否有环
Node fast = head;
Node slow = head;
if (fast==null) {
return false;
}
while(fast!=null&&fast.next!=null) {
fast=fast.next.next;
slow=slow.next;
if (fast==slow) {
return true;
}
}
return !(fast==null||fast.next==null);
}
public Node FindLoopPort(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 deleteNode(Node n) {//在不知道头指针的情况下删除指定节点
if(n==null||n.next==null) {
return false;
}
int tmp = n.data;
n.data = n.next.data;
n.next.data = tmp;
n.next=n.next.next;
return true;
}
public boolean isInteresect(Node h1,Node h2) {//判断两个表是否相交
if(h1==null||h2==null)
return false;
Node tail1=h1;
//找到链表h1的最后一个节点
while(tail1.next!=null) {
tail1=tail1.next;
}
Node tail2 = h2;
//找到链表h2的最后一个节点
while(tail2.next!=null) {
tail2=tail2.next;
}
/*
if(tail1==tail2) {
return true;
}else {
return false;
}
*/
return tail1==tail2;
}
public static Node getFirstMeetNode(Node h1,Node h2) {//找到两个链表相交的第一个节点
//判断两个链表是否相交
if(h1==null||h2==null)
return null;
Node tail1=h1;
int len1=1;
//找到链表h1的最后一个节点
while(tail1.next!=null) {
tail1=tail1.next;
len1=len1+1;
}
Node tail2 = h2;
int len2=1;
//找到链表h2的最后一个节点
while(tail2.next!=null) {
tail2=tail2.next;
len2=len2+1;
}
if(tail1!=tail2) {
return null;
}
Node t1=h1;
Node t2=h2;
if(len1>=len2) {
int len=len1-len2;
for(int i=0;i<len;i++) {
t1=t1.next;
}
}else {
int len=len2-len1;
for(int i=0;i<len;i++) {
t2=t2.next;
}
}
while(t1!=t2) {
t1=t1.next;
t2=t2.next;
}
return t1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyList it = new MyList();
it.addNode(3);
it.addNode(4);
it.addNode(6);
it.addNode(9);
it.addNode(8);
//it.outputNode();
//it.findElem(head,4);//返回倒数第4值
//System.out.println(+head.val);
//it.ReverseIteratively(head);
//it.outputNode();
//it.printListReversely(head);
//it.SearchMid(head);
}
}