当前位置:网站首页>Sword finger offer merges two sorted lists
Sword finger offer merges two sorted lists
2022-06-24 23:34:00 【ciwei24456】
describe
Enter two incremental linked lists , The length of a single linked list is n, Merge these two linked lists and make the nodes in the new linked list still be sorted incrementally .
Such as the input {1,3,5},{2,4,6} when , The combined linked list is {1,2,3,4,5,6}, So the corresponding output is {1,2,3,4,5,6}, The conversion process is shown in the figure below :
Their thinking :
A special case : If a linked list is empty , Return to another linked list
If pHead1 Node value ratio pHead2 Small , The next node should be pHead1, should return pHead1, stay return Before , Appoint pHead1 The next node of should be pHead1.next and pHead2 The merged head node of the two linked lists
If pHead1 Node value ratio pHead2 Big , The next node should be pHead2, should return pHead2, stay return Before , Appoint pHead2 The next node of should be pHead1 and pHead2.next The merged head node of the two linked lists
Python Code implementation
class Solution:
# Return to merged list
def Merge(self, pHead1, pHead2):
# The end condition of recursion is that there is an end , Return no None That's good
if not pHead1:
return pHead2
if not pHead2:
return pHead1
# Give Way phead1 Point to the small , Exchange if necessary
if pHead1.val > pHead2.val:
pHead1,pHead2 = pHead2,pHead1
# recursive procedure
pHead1.next = self.Merge(pHead1.next,pHead2)
return pHead1
边栏推荐
- 378. 骑士放置
- Continuous soul torture from two MySQL indexes of interviewers
- Idea creation module prompt already exists
- 22map introduction and API
- Morris traversal
- R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses exp function, confint function and coef function to obtain the confidence
- 六大行数据治理现状盘点:治理架构、数据标准与数据中台(2022.04)
- The dplyr package select function of R language moves the specified data column in the dataframe data to the first column (the first column) in the dataframe data column
- [basic knowledge] ~ half adder & full adder
- Actipro WPF Controls 2022.1.2
猜你喜欢

go 语言指针,值引用和指针引用
![[basic knowledge] ~ half adder & full adder](/img/06/7f1ede65dca527c8630285b587a4ba.png)
[basic knowledge] ~ half adder & full adder

我的为人处事真的有问题吗?
![[JS] - [string - application] - learning notes](/img/dc/f35979b094f04c0ee13b3354c7741d.png)
[JS] - [string - application] - learning notes

Morris traverse
![[JS] - [tree] - learning notes](/img/62/de4fa2a7c5e52c461b8be4a884a395.png)
[JS] - [tree] - learning notes

Detailed explanation of online group chat and dating platform project (servlet implementation)

Huawei machine learning service speech recognition function enables applications to paint "sound" and color

Fibonacci

国内有哪些好的智能家居品牌支持homekit?
随机推荐
Ganglia 的安装与部署
Use of types, values, namespaces, combinations, etc. in typescript
js监听页面或元素scroll事件,滚动到底部或顶部
RT-thread使用rt-kprintf
Hyperledger Fabric 2. X dynamic update smart contract
379. 捉迷藏
[JS] - [tree] - learning notes
7-2 construction of binary tree by post order + middle order sequence
Morris traversal
R language dplyr package group_ By function and summarize_ The at function calculates the dataframe to calculate the number of counts and the mean value of different groups (summary data by category v
How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
年薪百万,7年测试经验:守在一个还算不错的赛道,慢慢积累,等风来
常用正则表达式
Fibonacci
7-6 laying oil well pipeline
Why is it that the "Zhongtai" that was originally eaten by civil engineering is no longer fragrant?
UNION ALL UNION FULL JOIN
华为机器学习服务语音识别功能,让应用绘“声”绘色
Continuous soul torture from two MySQL indexes of interviewers
7-5 maximal submatrix sum problem