当前位置: 首页 > news >正文

力扣算法题1

1.反转链表

public ListNode ReverseList (ListNode head) {

        if(head==null){

            return null;

        }

        ListNode newHead = new ListNode(-1);

        ListNode cur = head;

        while(cur!=null){

            ListNode next = cur.next;

            cur.next = newHead.next;

            newHead.next = cur;

            cur = next;

        }

        return newHead.next;

    }

   public ListNode ReverseList (ListNode head) {

        if(head==null||head.next==null){

            return head;

        }

        ListNode newHead = ReverseList(head.next);

        head.next.next = head;

        head.next = null;

        return newHead;

    }

指定区间内反转链表

public ListNode reverse(ListNode head,ListNode end){

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

          return head;

        ListNode newHead = new ListNode(-1);

        ListNode cur = head;

        while(cur!=end){

            ListNode next = cur.next;

            cur.next = newHead.next;

            newHead.next = cur;

            cur = next;

       }

       return newHead.next;

    }

    public ListNode reverseBetween (ListNode head, int m, int n) {

      ListNode newHead = new ListNode(-1);  

      newHead.next = head;

      ListNode prev = newHead;

      ListNode cur = head;

      for(int i = 0;i <m-1;i++){

        prev = prev.next;

        cur = cur.next;

      }

      ListNode next = head;

      ListNode tmp = cur;

      for(int i = 0;i<n;i++){

        next = next.next;

      }

      ListNode ret = reverse(cur,next);

      prev.next = ret;

      tmp.next = next;

      return newHead.next;

    }

  1. 合并两个排序的链表(2种方法)

public ListNode Merge (ListNode pHead1, ListNode pHead2) {

        ListNode newHead = new ListNode(-1);

        ListNode tail = newHead;

        while(pHead1!=null&&pHead2!=null){

           if(pHead1.val<pHead2.val){

              tail.next = pHead1;

              pHead1=pHead1.next;

              tail=tail.next;

           }else{

              tail.next = pHead2;

              pHead2=pHead2.next;

              tail=tail.next;

           }

        }

        while(pHead1!=null){

           tail.next=pHead1;

           pHead1=pHead1.next;

           tail=tail.next;

        }        

        while(pHead2!=null){

           tail.next=pHead2;

           pHead2=pHead2.next;

           tail=tail.next;

        }      

        return newHead.next;

    }

 public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

       if(list1==null)

         return list2;

        if(list2==null)

         return list1;

        if(list1.val<list2.val){

            list1.next = mergeTwoLists(list1.next,list2);

            return list1;

        }else{

            list2.next = mergeTwoLists(list1,list2.next);

            return list2;

        }

    }

4.合并k个已排序的链表(2种方法)

public ListNode mergeKLists (ArrayList<ListNode> lists){

        return merge(lists,0,lists.size()-1);

    }

    public ListNode merge(ArrayList<ListNode> lists,int left, int right){

        if(left>right)

          return null;

        if(left==right)

          return lists.get(left);

        int mid = (left+right)/2;

        ListNode l1 = merge(lists,left,mid);

        ListNode l2 = merge(lists,mid+1,right);

        return Merge(l1,l2);

    }

   public ListNode mergeKLists2 (ArrayList<ListNode> lists){

       PriorityQueue<ListNode> heap = new PriorityQueue<>((v1,v2)->v1.val-v2.val);

       for(ListNode l:lists){

         if(l!=null)

          heap.offer(l);

       }

       ListNode newHead = new ListNode(-1);

       ListNode cur = newHead;

       while(!heap.isEmpty()){

         ListNode node = heap.poll();

         cur.next = node;

         cur = cur.next; 

         if(node.next!=null)

           heap.offer(node.next);

       }

       return newHead.next;

    }

    //=======================================================

    public ListNode Merge (ListNode pHead1, ListNode pHead2) {

        ListNode newHead = new ListNode(-1);

        ListNode tail = newHead;

        while(pHead1!=null&&pHead2!=null){

           if(pHead1.val<pHead2.val){

              tail.next = pHead1;

              pHead1=pHead1.next;

              tail=tail.next;

           }else{

              tail.next = pHead2;

              pHead2=pHead2.next;

              tail=tail.next;

           }

        }

        while(pHead1!=null){

           tail.next=pHead1;

           pHead1=pHead1.next;

           tail=tail.next;

        }        

        while(pHead2!=null){

           tail.next=pHead2;

           pHead2=pHead2.next;

           tail=tail.next;

        }      

        return newHead.next;

    }

5.判断是否有环

 public boolean hasCycle(ListNode head) {

        if(head==null){

            return false;

        }

        ListNode fast = head;

        ListNode slow = head;

        while(fast!=null&&fast.next!=null){

            fast = fast.next.next;

            slow = slow.next;

            if(fast == slow){

                return true;

            }

        }

        return false;

    }

相关文章:

  • JAVASCRIPT 前端数据库-V6--仙盟数据库架构-—-—仙盟创梦IDE
  • 横向对比npm和yarn
  • 省略号和可变参数模板
  • 五年级数学知识边界总结思考-下册
  • Axure Rp 11 安装、汉化、授权
  • Keil Mdk新建stm32工程找不到对应芯片开发包的解决方法
  • 代码规范和架构【立芯理论一】(2025.06.08)
  • 【判断自整除数】2022-4-6
  • 【题解-洛谷】B3626 跳跃机器人
  • 二叉树“倒着看”:层次遍历的反向打开方式
  • 车载诊断架构 --- 整车诊断数据管理策略
  • Cherry-Studio搭建个人知识库智能体
  • 一个一键生成知识讲解类教育视频的ai工具
  • 用通俗的话解释下MCP是个啥?
  • PyCharm和VS Code哪个更适合初学者
  • RootSIFT的目标定位,opencvsharp。
  • DAY 25 异常处理
  • 基于贝叶斯网络构建结构方程_TomatoSCI分析日记
  • 【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统
  • cookie session和token的区别
  • 装饰公司怎样做网站/无锡seo培训
  • 湖南郴州疫情最新数据/长沙正规seo优化公司
  • 外贸网站建设排名/昆山seo网站优化软件
  • l兰州网站建设/网站seo策划方案实例
  • 本地人wordpress怎么同步到服务器/济南seo外贸网站建设
  • microsoft做网站/线上广告