925打卡

1. 合并两个有序链表(21)

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

/**
 * 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 res = new ListNode(0);
        ListNode cur = res;
        while (list1!=null || list2!=null){
            if ( list1 ==null ){
                while (list2!=null){
                cur.next=new ListNode(list2.val);
                    list2=list2.next;
                    cur = cur.next;}
                
            }
            if ( list2 ==null ){
                while (list1!=null){
                    cur.next=new ListNode(list1.val);
                    list1=list1.next;
                    cur = cur.next;}
            }

            if(list1!=null && list2!=null){
                if (list1.val<=list2.val){
                    cur.next =new ListNode(list1.val);
                    list1=list1.next;
                    cur = cur.next;
                }else {
                    cur.next = new ListNode(list2.val);
                    list2=list2.next;
                    cur = cur.next;
                }
            }
        }
        return  res.next;
    
    }
}

2. 生成有效的括号(22)

数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

class Solution {
    public List<String> generateParenthesis(int n) {
   ArrayList<String> res = new ArrayList<>();
        if (n==0)
            return res;
        dfs("",n,n,res);
        return res;
    }

    /**
     *
     * @param s
     * @param left  左括号可用数量
     * @param right 右括号可用数量
     * @param res
     */
    private void dfs(String s, int left, int right, ArrayList<String> res) {
        //当前左右括号都有大于 0 个可以使用的时候,才产生分支
        if(left ==0 && right==0){
            res.add(s); //在左边和右边剩余的括号数都等于 0的时候结算
            return;
        }
        //产生右分支的时候,还受到左分支的限制,右边剩余可以使用的括号数量一定得在严格大于左边剩余的数量的时候,才可以产生分支;
        if(left>right){
            return;
        }
        //产生左分支的时候,只看当前是否还有左括号可以使用
        if(left>0){
            dfs(s+"(",left-1,right,res);
        }
        if(right>0){
            dfs(s+")",left,right-1,res);
        }


    }
}

3. 合并k个升序链表(23)

给你一个链表数组,每个链表都已经按升序排列。

思想: 分治+ 合并

/**
 * 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 mergeKLists(ListNode[] lists) {
   return merge(lists, 0, lists.length - 1);
    }

    private ListNode merge(ListNode[] lists, int left, int right) {
        if (left == right)
            return lists[left];
        if (left > right) {
            return null;
        }
        int mid = (left + right) >> 1;
        return merge2Lists(merge(lists, left, mid), merge(lists, mid + 1, right));
    }

    public ListNode merge2Lists(ListNode list1, ListNode list2) {
        if (list1 == null || list2 == null)
            return list1 == null ? list2 : list1;

        ListNode res = new ListNode(0);
        ListNode cur = res;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                cur.next = new ListNode(list1.val);
                list1 = list1.next;
            } else {
                cur.next = new ListNode(list2.val);
                list2 = list2.next;
            }
            cur = cur.next;
        }
        cur.next = (list1 == null) ? list2 : list1;
        return res.next;
    }
}

4. 两两交换链表节点(24)

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

思想: 迭代,两两交换,需要保存上一节点

/**
 * 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 swapPairs(ListNode head) {
              ListNode cur = head;
        ListNode pre = new ListNode(0);
        ListNode pres=pre;
        if(head==null || head.next ==null)
            return head;

        while (cur!=null&& cur.next !=null){
            ListNode temp = cur.next;
            cur.next = temp.next;
            temp.next = cur;
            pre.next = temp;
            cur = cur.next;
            pre = pre.next.next;
        }
        return pres.next;
    }
}

5. k个一组翻转(25)

给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表

思想: 反转

/**
 * 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 reverseKGroup(ListNode head, int k) {
   ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode end = dummy;

        while (end.next != null) {
            for (int i = 0; i < k && end != null; i++) {
                end = end.next;
            }
            if (end == null) break;
            ListNode start = pre.next;
            ListNode next = end.next;
            end.next = null;
            pre.next = reverse(start);
            start.next = next;
            pre = start;
            end = pre;
        }
      return dummy.next;

    }

    private ListNode reverse(ListNode head) {
        ListNode res = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = res;
            res = cur;
            cur = next;
        }
        return res;
    }
}

 

posted @ 2023-09-25 14:27  forever_fate  阅读(18)  评论(0)    收藏  举报