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

算法专题八: 链表

1.两数相加

题目链接:2. 两数相加 - 力扣(LeetCode)

/*** 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 cur1=l1,cur2=l2;ListNode newHead = new ListNode(0);ListNode prev=newHead;int t=0;while(cur1!=null || cur2!=null || t!=0){if(cur1!=null){t+=cur1.val;cur1=cur1.next;}if(cur2!=null){t+=cur2.val;cur2=cur2.next;}prev.next=new ListNode(t % 10);prev=prev.next;t/=10;//如果有进位,使t为1,加入下次的计算}return newHead.next;}
}

2.两两交换链表中的节点

题目链接:24. 两两交换链表中的节点 - 力扣(LeetCode)

/*** 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) {if(head==null || head.next==null){return head;}ListNode newHead=new ListNode(0);newHead.next=head;ListNode prev=newHead;ListNode cur=prev.next;ListNode next=cur.next;ListNode nnext=next.next;while(cur!=null && next!=null){next.next=cur;cur.next=nnext;prev.next=next;prev=cur;cur=nnext;if(cur!=null){next=cur.next;}if(next!=null){nnext=next.next;}}return newHead.next;}
}

3.重排列表

题目链接:143. 重排链表 - 力扣(LeetCode) 

头插法初始化 

/*** 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 void reorderList(ListNode head) {if(head==null || head.next==null || head.next.next==null){return ;}// 找到中间的节点ListNode fast=head;ListNode slow=head;while(fast!=null && fast.next!=null){slow=slow.next;fast=fast.next.next;}ListNode head2=new ListNode();ListNode cur=slow.next;slow.next=null;while(cur!=null){ListNode next=cur.next;    cur.next=head2.next;head2.next=cur;cur=next;  }// 合并两个列表ListNode cur1=head;ListNode cur2=head2.next;ListNode ret=new ListNode();ListNode prev=ret;while(cur1!=null){// 合并前一部分prev.next=cur1;prev=cur1;cur1=cur1.next;// 合并后一部分if(cur2!=null){prev.next=cur2;prev=cur2;cur2=cur2.next;}}}
}

4.合并k个升序列表

题目链接:23. 合并 K 个升序链表 - 力扣(LeetCode)

方法一:使用优先级队列来做

/*** 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) {// 创建一个小顶堆,元素从小到大进行排列PriorityQueue<ListNode> heap=new PriorityQueue<>((v1,v2) -> v1.val - v2.val);for(ListNode l:lists){if(l!=null){heap.offer(l);}}ListNode ret=new ListNode(0);ListNode result=ret;while(!heap.isEmpty()){ListNode t=heap.poll();result.next=t;result=t;if(t.next!=null){heap.offer(t.next);}}return ret.next;}
}

 方法二:采用分治,递归的方法

/*** 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 mergeSort(lists,0,lists.length-1);}public ListNode mergeSort(ListNode[] lists,int left,int right){if(left>right){return null;}if(left==right){return lists[left];}int mid=(left+right)/2;ListNode l1=mergeSort(lists,left,mid);ListNode l2=mergeSort(lists,mid+1,right);// 把两个列表进行排序return mergeTwoList(l1,l2);}public ListNode mergeTwoList(ListNode l1,ListNode l2){if(l1==null){return l2;}if(l2==null){return l1;}ListNode newHead=new ListNode(0);ListNode cur1=l1, cur2=l2, result=newHead;while(cur1!=null && cur2!=null){if(cur1.val>cur2.val){result.next=cur2;result=cur2;cur2=cur2.next;}else{result.next=cur1;result=cur1;cur1=cur1.next;}}if(cur1!=null){result.next=cur1;}if(cur2!=null){result.next=cur2;}return newHead.next;}
}

5.K个一组列表翻转

题目链接:25. K 个一组翻转链表 - 力扣(LeetCode)

/*** 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 cur=head;int n=0;while(cur!=null){cur=cur.next;n++;}n/=k;ListNode newHead=new ListNode(0);ListNode prev=newHead;cur=head;for(int i=0;i<n;i++){ListNode tmp=cur;for(int j=0;j<k;j++){ListNode ret=cur.next;cur.next=prev.next;prev.next=cur;cur=ret;}prev=tmp;}prev.next=cur;return newHead.next;}
}

 

希望对大家有所帮助!!!!

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

相关文章:

  • scanf 读取字符串
  • 本地密码生成管理工具,自定义生成密码
  • Vue3组件生成唯一标识符方法
  • 16.vue.js watch()和watchEffect()的对比?(追踪依赖)(3)
  • 华为OD机考-货币单位换算-字符串(JAVA 2025B卷)
  • CMake 构建系统概述
  • LeetCode - 153. 寻找旋转排序数组中的最小值
  • Hive SQL执行流程深度解析:从CLI入口到执行计划生成
  • 计网复习知识(16)传输层及其协议功能
  • 贝塞尔曲线:优雅的数学艺术
  • C# 解析 URL URI 中的参数
  • OpenWrt | 解决NTFS格式的硬盘意外断电之后无法再次挂载的问题
  • 轻量免安装 透明背景图标一键提取,系统文件图标随取随用
  • NGINX 四层共享内存区同步模块实战 `ngx_stream_zone_sync_module`
  • qml显示svg矢量图形
  • FreeRTOS的低功耗Tickless模式
  • RLHF调参实战手册:实用Trick、现象排查与解决思路(持续更新)
  • 动态BGP服务器的用途都有什么?
  • Softhub软件下载站实战开发(二):项目基础框架搭建
  • 萌系盲盒陷维权风暴,Dreams委托David律所已立案,速避雷
  • 历史数据分析——贵州茅台
  • LeetCode[106]从中序和后序遍历序列构造二叉树
  • Sngine 4.0.4海外社交平台PHP源码 – 多语言支持短视频和博客订阅(源码下载)
  • [学习] 多项滤波器在信号插值和抽取中的应用:原理、实现与仿真(完整仿真代码)
  • 使用Three.js创建炫酷的3D玻璃质感动态效果
  • 大小端的区别
  • MiniCPM4端侧AI模型
  • 机器学习算法_支持向量机
  • 图数据库(TuGraph)
  • DataX 框架学习笔记