当前位置:网站首页>[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;
}
}

边栏推荐
- C语言犄角旮旯的知识之指针
- (Reprinted) plantuml Quick Guide
- ArcGIS JS customizes the accessor and uses the watchutils related method to view the attribute
- Using identity framework to realize JWT identity authentication and authorization in.Net 6.0
- QT uses semaphores to control threads (qsemaphore)
- XSS知识点和20字符短域名绕过
- Modify the conf file through sed
- Qt使用信号量控制线程(QSemaphore)
- ArcGIS JS自定义Accessor,并通过watchUtils相关方法watch属性
- Qt多线程中槽函数在哪个线程里执行分析
猜你喜欢
![[Google] solve the problem that Google browser does not pop up the account and password save box and cannot save login information](/img/b3/2592e941f5d8f3505fb9763e8947a6.png)
[Google] solve the problem that Google browser does not pop up the account and password save box and cannot save login information

After being accidentally dragged into QQ fraud group

Digital management insight into retail and e-commerce operations -- Introduction to digital management

Information system project manager must recite the core examination site (41) risk management plan

QT uses semaphores to control threads (qsemaphore)

Merge two sorted linked lists - two questions per day

【13】加法器:如何像搭乐高一样搭电路(上)?

DNA cuinseqds near infrared CuInSe quantum dots wrapped deoxyribonucleic acid DNA

CLion调试redis6源码

Copper indium sulfide CuInSe2 quantum dots modified DNA (deoxyribonucleic acid) DNA cuinse2qds (Qiyue)
随机推荐
What if the computer file cannot be deleted?
【花书笔记】 之 Chapter01 引言
【13】加法器:如何像搭乐高一样搭电路(上)?
Qt多线程中槽函数在哪个线程里执行分析
JS card cascading style image switching JS special effect
It has been rectified seven times and took half a month. Painful EMC summary
DCL singleton mode
Elaborate on common mode interference and differential mode interference
Using identity framework to realize JWT identity authentication and authorization in.Net 6.0
Pytorch的冻结以及解冻
非关系型数据库之Redis【redis安装】
Redis of non relational database [detailed setup of redis cluster]
Merge two sorted linked lists - two questions per day
Spiral matrix
The first common node of two linked lists -- two questions per day
Adjust the array order so that odd numbers precede even numbers - two questions per day
【17】建立数据通路(上):指令+运算=CPU
Brief introduction to ThreadLocal class
Mysql, how many columns can be used to create an index?
Qt使用信号量控制线程(QSemaphore)