数据结构—链表的操作小结

来源:互联网 发布:哪个炒股软件最好 编辑:程序博客网 时间:2024/05/29 19:27

总结一下链表的相关操作

1判断是否有环 

2找到环的交界 

3返回环的长度 

4两个链表的公共节点 

5链表逆置 

6带有random指针的链表复制

import java.util.HashMap;import java.util.Map;/** * Created by root on 17-11-9. */class Node{    int val;    Node next;}class Node3{    int val;    Node3 next;    Node3 random;}public class 链表 {    public static void main(String args[]){    }    //判断是否是环--一个快指针,一个慢指针,快指针是慢指针速度的两倍,指针若能相遇,则存在环    public boolean isCircle(Node head){        Node slow = head;        Node fast = head;        while (fast!=null&&fast.next!=null){            slow=slow.next;            fast=fast.next.next;            if(slow==fast){                return true;            }        }        return false;    }    //找到环的起始位置--相遇后,slow指针放到头,slowfast一起移动到相遇    public Node FindCircle(Node head){        Node slow = head;        Node fast = head;        while (fast!=null&&fast.next!=null){            slow=slow.next;            fast=fast.next.next;            if (slow==fast){                break;            }        }        if (fast==null||fast.next==null){            return null;        }        slow=head;        while (slow!=fast){            slow=slow.next;            fast=fast.next;        }        return slow;    }    //返回环的长度--相遇以后,一个不动,一个一步一步的移动到相遇,这段长度就是    public int CircleLength(Node head){        Node slow = head;        Node fast = head;        while (fast!=null&&fast.next!=null){            slow=slow.next;            fast=fast.next.next;            if (slow==fast){                break;            }        }        if (fast==null||fast.next==null){            return 0;        }        fast=fast.next;        int count=1;        while (fast!=slow){            fast=fast.next;            count++;        }        return count;    }    //找到两个节点的公共节点,先进行长度作差,长的先移动,然后同时移动进行判断是否重叠    public Node CommonNode(Node node1,Node node2){        if (node1==null||node2==null)return null;        Node head1=node1;        Node head2=node2;        int count1=0,count2=0;        while (head1!=null){            head1=head1.next;            count1++;        }        while (head2!=null){            head2=head2.next;            count2++;        }        int sub =Math.abs(count1-count2);        head1=node1;        head2=node2;        if(count2>count1){            while (sub--!=0){                head2=head2.next;            }        }else{
            while (sub--!=0){                head1=head1.next;            }        }

        while (head1!=null&&head2!=null){            if (head1==head2)return head1;            else {                head1=head1.next;                head2=head2.next;            }        }        return null;    }    //逆置链表,头插法    Node ReverseList(Node head){        Node current = head.next;        Node next = current.next;        head.next=null;        while (current!=null){            current.next=head.next;            head.next=current;            current=next;            next=current.next;        }        return head;    }    //复制带有random指针的链表,先构建hashmap,然后再执行操作    Node3 copyList(Node3 head){        Node3 l2,l1=head;        Map<Node3,Node3> ht = new HashMap<Node3,Node3>();        while (l1!=null){            l2=new Node3();            l2.val=l1.val;            ht.put(l1,l2);            l1=l1.next;        }        l1=head;        while (l1!=null){            l2=ht.get(l1);            l2.next=ht.get(l1.next);            l2.random=ht.get(l1.random);            l1=l1.next;        }        return ht.get(head);    }}



原创粉丝点击