当前位置:网站首页>[leetcode] 24. Exchange nodes in the linked list in pairs
[leetcode] 24. Exchange nodes in the linked list in pairs
2022-07-28 08:19:00 【Xiaoqu classmate】
24、 Two or two exchange nodes in a linked list
subject :
I'll give you a list , Two or two exchange the adjacent nodes , And return the head node of the linked list after exchange . You must complete this problem without modifying the value inside the node ( namely , Only node switching can be performed ).
Example 1:

Input :head = [1,2,3,4]
Output :[2,1,4,3]
Example 2:
Input :head = []
Output :[]
Example 3:
Input :head = [1]
Output :[1]
Tips :
The number of nodes in the linked list is in the range [0, 100] Inside
0 <= Node.val <= 100
Their thinking :
Use recursion to solve the problem , Mainly recursive trilogy :
- Find termination conditions : The termination condition of this question is obvious , When recursion arrives when the linked list is empty or there is only one element left in the linked list , There's no exchange , Naturally, it ends .
- Find the return value : The value returned to the upper level of recursion should be the sub linked list after the exchange is completed .
- A single process : Because recursion is doing the same thing repeatedly , So from a macro perspective , Just think about how a certain step is completed . We assume that the two nodes to be exchanged are head and next,next Should accept the sub linked list returned by the previous level ( Refer to the first 2 Step ). It is equivalent to a linked list with three nodes exchanging the first two nodes , It's easy , If you don't understand, draw a picture ok.
Reference code :
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode next = head.next;
head.next = swapPairs(next.next);
next.next = head;
return next;
}
}

边栏推荐
- What if you are prompted that your connection to this website is not a private connection?
- 0727~ sorting out interview questions
- awk从入门到入土(16)awk变量类型探讨--关于数字和string两种类型
- 【13】 Adder: how to build a circuit like Lego (Part 1)?
- 豪华版h5俄罗斯方块小游戏源码
- @The role of documented
- Parse tree structure JS
- Elaborate on common mode interference and differential mode interference
- 【花书笔记】 之 Chapter01 引言
- DNA cuinseqds near infrared CuInSe quantum dots wrapped deoxyribonucleic acid DNA
猜你喜欢

【活动报名】云原生技术交流 Meetup,8 月 6 日广州见

flowable工作流所有业务概念

Tensorflow uses deep learning (II)

Plantuml Usage Summary

XSS knowledge points and 20 character short domain name bypass

Is the salary of test / development programmers unbalanced? Busy life, all kinds of job hopping

快速搭建DMHS DM之间双向同步

JUC原子类: CAS, Unsafe、CAS缺点、ABA问题如何解决详解
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)](/img/86/8e97b4456e2ba9a8535debb099fee0.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(八)
随机推荐
protobuf 基本语法总结
Opentsdb time series database
DNA cuinseqds near infrared CuInSe quantum dots wrapped deoxyribonucleic acid DNA
[dry goods] 32 EMC standard circuits are shared!
Enum class
JUC原子类: CAS, Unsafe、CAS缺点、ABA问题如何解决详解
非关系型数据库之Redis【redis集群详细搭建】
JUC atomic class: CAS, unsafe, CAS shortcomings, how to solve ABA problems in detail
Flowable workflow all business concepts
Near infrared two region agzs quantum dots wrapped deoxyribonucleic acid dna|dna agzsqds (Qiyue)
DNA deoxyribonucleic acid modified platinum nanoparticles ptnps DNA | scientific research reagent
DNA modified noble metal nanoparticles | DNA deoxyribonucleic acid modified metal palladium Pd nanoparticles pdnps DNA
node(一)
Protobuf basic grammar summary
These mobile security browsers are more than a little easy to use
Can the variable modified by final be modified
Forward propagation of deep learning neural networks (1)
mysql,可以使用多少列创建索引?
OSPF comprehensive experiment (7.12)
非关系型数据库之Redis【redis安装】