当前位置:网站首页>Leetcode sword finger offer jz25 merges two sorted linked lists
Leetcode sword finger offer jz25 merges two sorted linked lists
2022-07-24 06:25:00 【Happy and at ease】

It is known that the two linked lists to be merged are incremental permutations , When merging into a new linked list, we can consider creating a pseudo header
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C Language declaration defines global variables. Please add static, Prevent duplicate definitions
*/
/**
*
* @param pHead1 ListNode class
* @param pHead2 ListNode class
* @return ListNode class
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
struct ListNode dummy; // Declare pseudo header pointer
struct ListNode * cur=&dummy; //cur That is, the pseudo header pointer
while(pHead1&&pHead2){ // As long as the two linked lists are not empty , Start looking for
if(pHead1->val<=pHead2->val){ // When pHead1 The value is less than pHead2, Link it to the new linked list
cur->next=pHead1;
pHead1=pHead1->next; // Then move the linked list back 1
}
else
{
cur->next=pHead2;
pHead2=pHead2->next; // Ditto and vice versa
}
cur=cur->next;
}
cur->next=pHead1==NULL?pHead2:pHead1; // The list is empty , if 1 Empty rule link 2, Otherwise link 1, Use macro judgment
return dummy.next; Return to the head of the new linked list
}边栏推荐
- Summary of ten common vulnerabilities (principle, harm, defense)
- Do not rent servers, build your own personal business website (3)
- Jenkins automated unattended operation (up / down)
- Heap overflow of kernel PWN basic tutorial
- IP笔记(11)
- 记一次高校学生账户密码的获取,从无到有
- 10大漏洞评估和渗透测试工具
- Li Kou 986. Intersection of interval lists
- Summary of common working methods (7S, SWOT analysis, PDCA cycle, smart principle, 6w2h, time management, WBS, 28 principles)
- 常见十大漏洞总结(原理、危害、防御)
猜你喜欢

服务器硬件及RAID配置实战

Summary of ten common vulnerabilities (principle, harm, defense)

IP lesson summary (3)
![Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]](/img/2a/43ba2839b842e0901a550d2883b883.png)
Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]

IP notes (12)

IP notes (10)

Machine learning & deep learning introduction information sharing summary

不租服务器,自建个人商业网站(3)
![[301] grotesque behavior - predictable irrationality](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[301] grotesque behavior - predictable irrationality

剑指offer JZ10斐波那契数列
随机推荐
一个测试经理/测试主管/测试总监的工作总结
Use and principle of spark broadcast variable and accumulator
A batch of interview questions and answers_ 20180403 latest arrangement
[214] what is an automation framework
[222] memory overflow and location
IP课笔记(4)
常见十大漏洞总结(原理、危害、防御)
UE4 replacement system 3. Final results
【219】app 测试和web测试的区别点?
Unity2d horizontal game jump real-time response
Dameng database_ Dmfldr tool instructions
leetcode剑指offer JZ25 合并两个排序的链表
IP笔记(11)
IP笔记(8)
【217】#!/usr/bin/env 的意义
IP class notes (5)
Machine learning & deep learning introduction information sharing summary
数据集和预训练模型
Using keras to realize LSTM time series prediction based on attention mechanism
IP notes (9)