链表基本问题集锦

自己整理了一些基本问题,没有好好整理,日后有问题再补充。



import java.util.Stack;

public class LinkList {

    public Node head;//头结点
    public Node current;

    // 方法1:向链表中添加数据
    public void add(int data) {

        if (head == null) {
            head = new Node(data);
            current = head;
        } else {
            current.next = new Node(data);
            current = current.next;
        }

    }

    /*//该方法构造有环链表时用
    public void add(Node node){

        if(node == null)
            return;
        if(head == null){
            head = node;
            current = head;
        }
        else{
            current.next = head;
            current = current.next;
        }
    }*/

    //方法2:遍历链表
    public void display(Node node){

        if(node == null)
            return;
        current = node;
        while(current != null){
            if(current.next == null)
            System.out.print(current.data+"\n");
            else
                System.out.print(current.data + "->");
            current = current.next;
        }

    }
    //方法3:获取单链表的长度
    public int getLength(Node head){

        if(head == null)
            return 0;
        else{
            int length=0;
            Node current = head;
            while(current != null){
                length++;
                current = current.next;
            }
            return length;
        }
    }
    //方法:查找倒数第k个结点
    //方法1:遍历
    public Node findLastNode(int index){

        if(head == null)
            return null;
        current = head;
        int size = 0;
        while(current != null){
            size++;
            current = current.next;
        }
        if(index<1||index>size)
            return null;
        current = head;
        for(int i=0;i<size-index;i++)
            current = current.next;
        return current;


    }

    //方法2:两个指针遍历

    public Node findLastNode(Node head,int index){

        if(head == null || index <1) 
            return null;
        Node first = head;
        Node second = head;

        for(int i=0;i<index-1;i++){
            first = first.next;
            if(first == null)
                return null;

        }

        while(first.next != null){
            first = first.next;
            second = second.next;

        }


        return second;



    }
    //寻找中间节点
    public Node findMidNode(Node head){

        if(head == null)
            return null;
        Node first = head;
        Node second = head;
        while(first != null && first.next != null){

            first = first.next.next;
            second = second.next;

        }

        return second;
    }
    //合并为一个有序递增链表
    public Node mergeList(Node head1,Node head2){
        if(head1 == null && head2== null)
            return null;
        else if(head1 != null && head2 == null)
            return head2;
        else if(head1 == null && head2 != null)
            return head1;
        else{

            Node current1 = head1;
            Node current2 = head2;
            Node head ;
            Node current;
            if(current1.data<current2.data){
                head = current1;
                current = head;
                current1 = current1.next;
            }
            else{
                head = current2;
                current = head;
                current2 = current2.next;
            }
            while(current1 != null && current2 != null){

                if(current1.data<current2.data){
                    current.next = current1;
                    current = current.next;
                    current1 = current1.next;
                }
                else if(current1.data > current2.data){
                    current.next = current2;
                    current = current.next;
                    current2 = current2.next;
                }
                else{

                    current.next = current1.next;
                    current = current.next;
                    current.next = current2.next;
                    current = current.next;
                    current1 = current1.next;
                    current2 = current2.next;

                }


            }


        if(current1 != null){
            current.next = current1;
        }
        if(current2 != null){
            current.next = current2;
        }

        return head;

        }
    }

    //链表反转
    public Node reverse(Node head){

        if(head == null || head.next == null)
            return head;

        Node current = head;
        Node next = null;
        Node reverseHead = null;
        while(current != null){
            next = current.next;
            current.next = reverseHead;
            reverseHead = current;
            current = next;
        }
        return reverseHead;

    }
    //从尾到头打印链表
    public void reverseDisplay(Node head){

        if(head == null)
            return;
        Node current = head;

        Stack<Node> stack = new Stack<Node>();
        while(current != null){
            stack.push(current);
            current = current.next;

        }

        for(int i=0; i< stack.size();i++)
            System.out.print(stack.pop().data + " ");


    }


    //递归打印,从尾到头 
    public void reverseDisplay1(Node head){
        if (head == null)
             return;

            reverseDisplay1(head.next);
            System.out.print(head.data+" ");

    }

    public boolean hasCycle(Node head){
        //如果链表为空,则返回false
        if(head == null)
            return false;
        //first每次移动两步,second每次移动一步
        Node first = head;
        Node second = head;

        //若链表无环,则必定first先到达链表终点
        //循环判定条件:链表结点为奇数,first是否为空;结点为偶数时,first->next是否为空
        while(first != null && first.next != null){

            first = first.next.next;
            second = second.next;
            //若first与second相遇,则代表链表有环
            if(first == second)
                return true;
        }

        return false;
    }

    //求环中相遇结点
    public Node cycleNode(Node head){
        //链表为空则返回null
        if(head == null)
            return null;

        Node first = head;
        Node second = head;
        while(first != null && first.next != null){

            first = first.next.next;
            second = second.next;
            //两指针相遇,则返回相遇的结点 
            if(first == second)
                return first;
        }
        //链表无环,则返回null
        return null;
    }

    public int getCycleLength(Node head){

        Node node = cycleNode(head);
        //node为空则代表链表无环
        if(node == null)
            return 0;
        int length=1;
        Node current = node.next;
        //再次相遇则循环结束
        while(current != node){
            length++;
            current = current.next;
        }

        return length;

    }

    public Node findIntersectNode(Node head1,Node head2){

        if(head1 == null || head2 == null)
            return null;

        int length1 = getLength(head1);
        int length2 = getLength(head2);

        if(length1 > length2){
            for(int i=0;i<length1-length2;i++)
                head1 = head1.next;
        }

        if(length1 < length2){
            for(int i=0;i<length2-length1;i++)
                head2 = head2.next;
        }

        while(head1!=null && head2!=null){

            if(head1 == head2)
                return head1;
            head1 = head1.next;
            head2 = head2.next;
        }

        return null;
    }


    /*//判断是否有环,有则返回相遇那个节点
    public Node hasCycle1(Node head){

        if(head == null)
            return null;
        Node first = head;
        Node second = head;
        while(first != null){
            first = first.next.next;
            second = second.next;
            if(first == second){
                return first;
            }
        }
        return null;

    }

    public int getCycleLength(Node head){

        Node node = this.hasCycle1(head);
        if(node == null)
            return 0;
        else{
            current = node.next;
            int length = 1;
            while(current != node){
                length++;
                current = current.next;
            }
            return length;
        }
    }*/

    public Node getCycleStart(Node head){
        //获取环的长度
        int length = getCycleLength(head);
        //长度为0 则代表无环
        if(length == 0)
            return null;
        else{

            Node first = head;
            Node second = head;
            //让first指针先移动环长度
            for(int i=1;i<=length;i++)
                first = first.next;
            while(second != first){
                first = first.next;
                second = second.next;
            }

            //两者相遇时,为环的入口点
            return first;
        }
    }
    public boolean isIntersect(Node head1,Node head2){

        if( head1 == null|| head2 == null )
            return false;

        while(head1 != null)
            head1 = head1.next;

        while(head2 != null)
            head2 = head2.next;
        if(head1 == head2)
            return true;
        else
            return false;


    }


    public boolean isIntersectWithCycle(Node head1,Node head2){
        //node1和node2保存环上的一点
        Node node1 = cycleNode(head1);
        Node node2 = cycleNode(head2);
        //如果不存在环上的一点,则代表无环
        if(node1 == null || node2 == null)
            return false;

        Node current = node1.next;
        while(current != node1){
            //若current与node2相遇,则代表两链表共享一个环即相交
            if(current == node2)
                return true;
            current = current.next;
        }

            return false;

    }



    class Node{

        int data;//数据域
        Node next;//指针域

        public Node(int data){

            this.data  = data;
        }
    }

    public static void main(String[] args){



        LinkList linkList = new LinkList();
        for(int i=1;i<=100;i++)
            linkList.add(i);

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值