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

力扣算法题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;

    }

http://www.xdnf.cn/news/12732.html

相关文章:

  • Vue部署到Nginx上及问题解决
  • 深入理解 React Hooks
  • 通过css实现正方体效果
  • 【免费数据】2005-2019年我国272个地级市的旅游竞争力多指标数据(33个指标)
  • C++11 右值引用
  • Pandas-如何正确将两张数据表进行合并
  • 自定义protoc-gen-go生成Go结构体,统一字段命名与JSON标签风格
  • 【Zephyr 系列 15】构建企业级 BLE 模块通用框架:驱动 + 事件 + 状态机 + 低功耗全栈设计
  • github开源协议选择
  • iview-admin静态资源js按需加载配置
  • STM标准库-TIM旋转编码器
  • JAVASCRIPT 前端数据库-V6--仙盟数据库架构-—-—仙盟创梦IDE
  • 深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
  • 2025年夏第九届河北工业大学程序设计校赛
  • Linux 上的 Tomcat 端口占用排查
  • 2025-06-08 思考-人被基因和社会关系双重制约
  • Psychopy音频的使用
  • 实验四:图像灰度处理
  • 自动化立体仓库堆垛机控制系统STEP7 OB1功能块
  • python打卡day48
  • 《解锁树莓派+Java:TinyML模型部署的性能飞升秘籍》
  • Java 面向对象进阶之多态:从概念到实践的深度解析
  • Windmill:开源开发者基础设施的革命者
  • Apache Spark详解
  • 【Pikachu】PHP反序列化RCE实战
  • 神经网络-Day48
  • 【threejs】每天一个小案例讲解:创建基本的3D场景
  • nodejs环境变量配置
  • 新手如何选择前端框架?
  • 【五子棋在线对战】三.数据管理模块实现