当前位置:网站首页>leetcode-02(链表题)
leetcode-02(链表题)
2022-07-06 19:34:00 【Fairy要carry】

迭代法:
思路:划分一个局部位置,然后用一个辅助节点和辅助指针,比较大小然后辅助指针去连即可,每连一次位置往后推;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
//辅助迭代法
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode help=new ListNode(-1);
ListNode pre=help;//辅助指针
//遍历
while (l1 != null && l2 != null){
if(l1.val<=l2.val){
//进行连接
pre.next=l1;
pre=pre.next;
l1=l1.next;
}else{
pre.next=l2;
pre=pre.next;
l2=l2.next;
}
}
//对剩下节点直接连
if(l1!=null){
pre.next=l1;
}
if(l2!=null){
pre.next=l2;
}
return help.next;
}
}递归解法
class Solution {
//递归
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//1.结束条件
if(l1==null){
return l2;
}
if(l2==null){
return l1;
}
//2.递归思路
if(l1.val<=l2.val){
l1.next=mergeTwoLists(l1.next,l2);
return l1;
}else{
l2.next=mergeTwoLists(l1,l2.next);
return l2;
}
}
}
递归解法,与上提类似
class Solution {
//递归
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
//1.结束条件
if(list1==null){
return list2;
}
if(list2==null){
return list1;
}
//2.递归思路
if(list1.val<=list2.val){
list1.next=mergeTwoLists(list1.next,list2);
return list1;
}else{
list2.next=mergeTwoLists(list1,list2.next);
return list2;
}
}
}
迭代法
注意指针的利用指向head,然后指向前一个结点,慢慢往后推
/**
* 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 reverseList(ListNode head) {
ListNode pre=null;
ListNode cur=head;
//每次循环进行两两交换
while(cur!=null){
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
快慢指针:
/**
* 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 middleNode(ListNode head) {
int n = 0;
ListNode cur = head;
while (cur != null) {
++n;
cur = cur.next;
}
int k = 0;
cur = head;
while (k < n / 2) {
++k;
cur = cur.next;
}
return cur;
}
}
边栏推荐
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
- Redis入门完整教程:复制原理
- S120驱动器基本调试步骤总结
- 6-6 vulnerability exploitation SSH security defense
- 【2022国赛模拟】多边形——计算几何、二分答案、倍增
- 【Socket】①Socket技术概述
- 从零安装Redis
- Utilisation de la promesse dans es6
- Niuke programming problem -- double pointer of 101 must be brushed
- Redis入门完整教程:问题定位与优化
猜你喜欢
Django database (SQLite) basic introductory tutorial

PSINS中19维组合导航模块sinsgps详解(时间同步部分)

Dotconnect for DB2 Data Provider

“零售为王”下的家电产业:什么是行业共识?

Software testing -- common assertions of JMeter interface testing

Left value, right value

Digital scrolling increases effect

Es6中Promise的使用

IDEA重启后无法创建Servlet文件的解决方案
![[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]](/img/f0/79e7915ba3ef32aa21c4a1d5f486bd.jpg)
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
随机推荐
6-6 vulnerability exploitation SSH security defense
Convert widerperson dataset to Yolo format
C language exercises_ one
Error in fasterxml tostringserializerbase
Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions
Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform
What management points should be paid attention to when implementing MES management system
Work of safety inspection
“零售为王”下的家电产业:什么是行业共识?
商城商品的知识图谱构建
KYSL 海康摄像头 8247 h9 isapi测试
Redis入门完整教程:复制拓扑
A complete tutorial for getting started with redis: problem location and optimization
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
Code line breaking problem of untiy text box
Use of promise in ES6
2022年信息安全工程师考试大纲
S120驱动器基本调试步骤总结
Es6中Promise的使用
Unity custom webgl packaging template