leecodecode【面试150】【2026.6.20/21打卡-java版本】

两数相加

要点:模拟,carry以及最后一个数要检查

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //头尾

        ListNode head = null;
        ListNode tail = null;
        //a+b/10.%10
        int carry=0;
        while(l1 != null || l2 != null){
            int a = l1 == null ?0 :l1.val;
            if(l1!= null)
            l1 = l1.next;
            int b = l2 == null ? 0 : l2.val;
            if(l2!=null)
            l2 = l2.next;

            int val = (a+b+carry)%10;
            carry = (a+b+carry)/10;
           
           if(head == null && tail ==null){
            head = tail = new ListNode(val);
           }else{
            tail.next  = new ListNode(val);
            tail = tail.next;
           }

           if(l1 == null && l2 ==null){
            if(carry != 0){
                tail.next = new ListNode(carry);
                tail = tail.next;
            }
           }


        }

        return head;

   


        
    }
}

合并两个有序链表

要点:dummy,然后模拟

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummy = new ListNode();
        ListNode ans = dummy;
        while(list1 != null && list2 != null){

            if(list1.val < list2.val){
                ans.next = list1;
                list1 = list1.next;
            }else{
                ans.next = list2;
                list2 = list2.next;
            }

            ans = ans.next;

        }

        ans.next =(list1 == null)? list2 :list1;

        return dummy.next;
        
    }
}

旋转链表

要点:三次反转

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        //三次反转
        if (head == null || head.next == null) return head; 
  

        ListNode list = head;
        int n = 0;
        while(list != null){
            list = list.next;
            n++;
        }
        ListNode list1 = head;

        k=k%n;
        if (k == 0) return head;
         // 无需旋转
        for (int i = 1; i < n - k; i++) { // 从 head 走 n-k-1 步
            list1 = list1.next;
        }


        ListNode list2 = list1.next;
        list1.next = null;

      //  list2 = reverse(list2);
        //head = reverse(head);
        //head.next = list2;

        //return reverse(head);
        ListNode leftHead = head;       // 保存原左半部分的头节点(反转后会变成尾节点)

        ListNode reversedRight = reverse(list2);   // 反转右半部分
        ListNode reversedLeft = reverse(leftHead); // 反转左半部分(返回新头)

        leftHead.next = reversedRight;  // 原左半部分的头节点现在成为尾,指向反转后的右半部分

        return reverse(reversedLeft);   // 整体反转
        
    }

    public ListNode reverse(ListNode node){
        ListNode pre = null;
        ListNode cur = node;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }

        return pre;

    }
}

分隔链表

要点:small,large 以及他们的dummy

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode small = new ListNode(0);
        ListNode smallHead = small;
        ListNode large = new ListNode(0);
        ListNode largeHead = large;
        while(head != null){
            if(head.val < x){
                small.next = head;
                small = small.next;
            }else{
                large.next = head;
                large = large.next;
            }
            head = head.next;
        }

        large.next = null;
        small.next = largeHead.next;

        return smallHead.next;
        
    }
}

碎碎念:后续会更新每天学习的八股和算法 题,开始准备秋招的第41/42天。努力连续更新100天!以后每天就按,秋招项目【java +agent】,科研,必做项目,算法,八股,锻炼身体来总结。

总结:脑子疼休息了两天

1.算法面试150 82/150 1h

2.秋招项目,【java 项目】,【修改,1h】

【agent 项目 】,【修改,1h】

3.科研要跑一下,无

4.检测项目,

6.背八股,无

7.锻炼身体,

反思:放假还是要保证正常作息