当前位置:网站首页>Leetcode-02 (linked list question)
Leetcode-02 (linked list question)
2022-07-07 03:12:00 【Fairy wants carry】

Iterative method :
Ideas : Divide a local location , Then use an auxiliary node and auxiliary pointer , Compare the size and then connect the auxiliary pointer , Push back every time ;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
// Auxiliary iteration method
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode help=new ListNode(-1);
ListNode pre=help;// Auxiliary pointer
// Traverse
while (l1 != null && l2 != null){
if(l1.val<=l2.val){
// Connect
pre.next=l1;
pre=pre.next;
l1=l1.next;
}else{
pre.next=l2;
pre=pre.next;
l2=l2.next;
}
}
// Connect the remaining nodes directly
if(l1!=null){
pre.next=l1;
}
if(l2!=null){
pre.next=l2;
}
return help.next;
}
}The recursive method
class Solution {
// recursive
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//1. The end condition
if(l1==null){
return l2;
}
if(l2==null){
return l1;
}
//2. Recursive thinking
if(l1.val<=l2.val){
l1.next=mergeTwoLists(l1.next,l2);
return l1;
}else{
l2.next=mergeTwoLists(l1,l2.next);
return l2;
}
}
}
The recursive method , Similar to the above
class Solution {
// recursive
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
//1. The end condition
if(list1==null){
return list2;
}
if(list2==null){
return list1;
}
//2. Recursive thinking
if(list1.val<=list2.val){
list1.next=mergeTwoLists(list1.next,list2);
return list1;
}else{
list2.next=mergeTwoLists(list1,list2.next);
return list2;
}
}
}
Iterative method
Pay attention to the use of pointer pointing head, Then point to the previous node , Push back slowly
/**
* 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;
// Each cycle is exchanged in pairs
while(cur!=null){
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
Speed pointer :
/**
* 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;
}
}
边栏推荐
- [cpk-ra6m4 development board environment construction based on RT thread studio]
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
- 【colmap】已知相机位姿情况下进行三维重建
- 商城商品的知识图谱构建
- [tools] basic concept of database and MySQL installation
- 凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
- [socket] ① overview of socket technology
- 杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】
- New benchmark! Intelligent social governance
- 腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》
猜你喜欢

Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions

Household appliance industry under the "retail is king": what is the industry consensus?

MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua

uniapp的表单验证

Redis入門完整教程:問題定比特與優化

How-PIL-to-Tensor
![[cpk-ra6m4 development board environment construction based on RT thread studio]](/img/08/9a847c73d6da6fc74d84af56897752.png)
[cpk-ra6m4 development board environment construction based on RT thread studio]

尚硅谷JVM-第一章 类加载子系统

上个厕所的功夫,就把定时任务的三种调度策略说得明明白白

When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
随机推荐
Laravel php artisan 自动生成Model+Migrate+Controller 命令大全
leetcode-02(链表题)
【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
从0开始创建小程序
Data analysis from the perspective of control theory
Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application
杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】
sshd[12282]: fatal: matching cipher is not supported: [email protected] [preauth]
How does C language (string) delete a specified character in a string?
Summary of research status of inertial navigation calibration at home and abroad (abridged version)
HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路
c语言字符串排序
[tools] basic concept of database and MySQL installation
首届“量子计算+金融科技应用”研讨会在京成功举办
Simple bubble sort
oracle连接池长时间不使用连接失效问题
Redis getting started complete tutorial: replication topology
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
Opencv environment, and open a local PC camera.
Cocos2d-x box2d physical engine compilation settings