Java-链表2

一、相交链表的起始节点
在这里插入图片描述

public class Solution {
   //找两个链表的公共节点
    public  ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        int lenA = 0;
        int lenB = 0;
        ListNode PL = headA;//长
        ListNode PS = headB;//短
        while (PL != null) {
            lenA++;
            PL = PL.next;
        }
        while (PS != null) {
            lenB++;
            PS = PS.next;
        }
        PL = headA;
        PS = headB;
        int len = lenA - lenB;
        if (len < 0) {
            PL = headB;
            PS = headA;
            len = lenB - lenA;
        }
        //PL指向长的,PS指向短的
        //PL走了差值步
        while (len > 0) {
            PL = PL.next;
            len--;
        }
        while (PL != null && PS != null && PL != PS) {
            PL = PL.next;
            PS = PS.next;
        }
        if (PL != null && PS != null && PL == PS) {
            return PS;
        }
        return null;
    }
}

二、判断链表是否有环
在这里插入图片描述

    //这里需要注意的是,上面的whlie循环跳出来有三种情况
    //1、fast指向null
    //2、slow指向null
    //3、相遇了
    //所以再这里要处理一下这样的情况
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast=head;
        ListNode 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 false;
        }
        return true;
    }
}

三、链表入环的第一个节点
在这里插入图片描述

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast=head;
        ListNode 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(fast!=slow){
            fast=fast.next;
            slow=slow.next;
        }
        return fast;
    }
}

四、链表分割:给定一个基准值x,把小于x的节点排在等于或者大于x的节点之前
在这里插入图片描述

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode cur=pHead;
        ListNode bs=null;
        ListNode be=null;
        ListNode as=null;
        ListNode ae=null;
        while(cur!=null){
            if(cur.val<x){
                //第一次插入
                if(bs==null){
                    bs=cur;
                    be=bs;
                }else{
                   ///已经有
                    be.next=cur;
                    be=be.next;
                }
            }else{
                if(as==null){
                    as=cur;
                    ae=as;
                }else{
                    ae.next=cur;
                    ae=ae.next;
                }
            }
            cur=cur.next;
        }
        if(bs==null){
            return as;
        }
        //bs!=null
        be.next=as;
        if(as!=null){
            ae.next=null;
        }
        return bs;
    }
}

五、有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。
给定两个链表ListNode* A,ListNode* B,请返回A+B的结果(ListNode*)
在这里插入图片描述

public class TestLinkedList {
        public ListNode plusAB(ListNode A, ListNode B) {

            //传入两个链表
            //先把链表转成整数进行计算
            Integer a = listNodeInvertIntValue(A);
            Integer b = listNodeInvertIntValue(B);

            //然后相加
            Integer ret = a + b;
            //最后再把结果反序插入到链表中
            return intValueRevertListNode(ret);


        }

        private ListNode intValueRevertListNode(Integer value) {
            char[] charArray = String.valueOf(value).toCharArray();
            ListNode node = new ListNode(Integer.parseInt(String.valueOf(charArray[charArray.length - 1])));
            ListNode cur = node;
          //整数反转存储到链表中
            for (int i = charArray.length - 2; i >= 0; i--) {
                ListNode newNode = new ListNode(Integer.parseInt(String.valueOf(charArray[i])));
                cur.next = newNode;
                cur = newNode;
            }
            return node;
        }


        private Integer listNodeInvertIntValue(ListNode P) {
            StringBuilder stringBuilder = new StringBuilder();
            ListNode cur = P;
            while (cur != null) {
                stringBuilder.append(cur.val);
                cur = cur.next;

            }
            return Integer.parseInt(stringBuilder.reverse().toString());
        }
    }
class Node {
    int val;
    Node next;

    public Node(int val) {
        this.val = val;
    }

}

猜你喜欢

转载自blog.csdn.net/Subuprogrammer/article/details/107407452