【LeetCode】刷题记录--单链表相关

21

java 复制代码
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) return list2;
        if (list2 == null) return list1;
        ListNode result = new ListNode(0);
        ListNode p = result;
        while (list1 != null && list2 != null){
            if (list1.val < list2.val){
                p.next = list1;
                list1 = list1.next;
            }else {
                p.next = list2;
                list2 = list2.next;

            }
            p = p.next;
        }
        if (list1 != null) p.next = list1;
        if (list2 != null) p.next = list2;
        return result.next;
    }

83

java 复制代码
public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        ListNode result = head;
        while (result.next != null){
            if (result.val == result.next.val){
                result.next = result.next.next;
            }else {
                result = result.next;
            }
        }
        return head;
    }

141

java 复制代码
public boolean hasCycle(ListNode head) {
//        if (head == null) return false;
//        HashMap<ListNode,Integer> linkListMap = new HashMap<ListNode, Integer>();
//        while (head.next != null){
//            if (linkListMap.get(head) == null){
//                linkListMap.put(head,head.val);
//            }else return true;
//            head = head.next;
//        }
//        return false;
        //-----------------------------------------------
        if (head == null) return false;
        ListNode fastPtr = head,slowPtr = head;
        while (fastPtr.next != null && fastPtr.next.next != null){
            fastPtr = fastPtr.next.next;
            slowPtr = slowPtr.next;


            if (fastPtr == slowPtr) return true;


        }
        return false;
        
    }

142

java 复制代码
public ListNode detectCycle(ListNode head) {
        if (head == null) return null;
        boolean test = false;
        ListNode fast = head,slow = head;
        while (fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                test = true;
                break;
            }
        }

        if (test){
            slow = head;
            while (slow != fast){
                slow = slow.next;
                fast = fast.next;
            }
            return slow;
        }
        return null;
    }

160

java 复制代码
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int L1=0,L2=0;
        ListNode la = headA,lb = headB;
        while (la != null){
            L1++;
            la = la.next;
        }
        while (lb != null){
            L2++;
            lb = lb.next;
        }
        if ((L1 - L2)>0){
            //a比b长
            for (int i = 0; i < (L1 - L2); i++) headA = headA.next;
            while (headA != headB) {
                headA = headA.next;
                headB = headB.next;
            }
            return headA;
        }else {
            //b比a长或相等
            for (int i = 0; i < (L2 - L1); i++) headB = headB.next;
            while (headB != headA){
                headA = headA.next;
                headB = headB.next;
            }
            return headA;
        }
    }

206 反转列表

java 复制代码
public ListNode reverseList(ListNode head) {
        ListNode currNode = head;
        ListNode preNode = null;
        while (currNode != null){
            ListNode next = currNode.next;
            currNode.next = preNode;
            preNode = currNode;
            currNode = next;
        }
        return preNode;
    }

234回文链表

java 复制代码
public boolean isPalindrome(ListNode head) {
        if (head == null) return false;
        ListNode fast = head,slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        if (fast != null){
            slow = slow.next;
        }
        slow = reverse(slow);
        fast = head;
        while (slow != null){
            if (fast.val != slow.val) return false;
            fast = fast.next;
            slow = slow.next;
        }
        return true;
}
public ListNode reverse(ListNode head){
        ListNode preNode = null;
        while (head != null){
            ListNode next = head.next;
            head.next = preNode;
            preNode = head;
            head = next;
        }
        return preNode;
    }

876链表的中间节点

java 复制代码
public ListNode middleNode(ListNode head) {
        ListNode fast = head,slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;//双指针

    }
相关推荐
GEEK零零七1 分钟前
Leetcode 1103. 分糖果 II
数学·算法·leetcode·等差数列
今天背单词了吗98017 分钟前
算法学习笔记:7.Dijkstra 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·数据结构·笔记·算法
重庆小透明1 小时前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
desssq2 小时前
力扣:70. 爬楼梯
算法·leetcode·职场和发展
clock的时钟2 小时前
暑期数据结构第一天
数据结构·算法
小小小小王王王3 小时前
求猪肉价格最大值
数据结构·c++·算法
岁忧3 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
BIYing_Aurora3 小时前
【IPMV】图像处理与机器视觉:Lec13 Robust Estimation with RANSAC
图像处理·人工智能·算法·计算机视觉
martian6655 小时前
支持向量机(SVM)深度解析:从数学根基到工程实践
算法·机器学习·支持向量机
孟大本事要学习5 小时前
算法19天|回溯算法:理论基础、组合、组合总和Ⅲ、电话号码的字母组合
算法