当前位置:网站首页>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;
}
}
边栏推荐
- 凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
- Matlab Error (Matrix dimensions must agree)
- SQL Tuning Advisor一个错误ORA-00600: internal error code, arguments: [kesqsMakeBindValue:obj]
- 左程云 递归+动态规划
- Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
- Unity使用MaskableGraphic画一条带箭头的线
- Kubernetes source code analysis (II) -- resource
- New benchmark! Intelligent social governance
- [tools] basic concept of database and MySQL installation
- Redis introduction complete tutorial: replication principle
猜你喜欢

密码学系列之:在线证书状态协议OCSP详解

leetcode

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

tensorboard的使用

The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing

Centerx: open centernet in the way of socialism with Chinese characteristics

Leetcode 77: combination
![[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]

杰理之播内置 flash 提示音控制播放暂停【篇】

Redis入門完整教程:問題定比特與優化
随机推荐
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
Install redis from zero
input_ delay
Netperf and network performance measurement
【无标题】
Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
【达梦数据库】备份恢复后要执行两个sql语句
首届“量子计算+金融科技应用”研讨会在京成功举办
QT Bluetooth: qbluetooth DeviceInfo
制作(转换)ico图标
Room rate system - login optimization
An error in SQL tuning advisor ora-00600: internal error code, arguments: [kesqsmakebindvalue:obj]
Cocos2d-x Box2D物理引擎编译设置
Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
Leetcode 77: combination
杰理之发射端在接收端关机之后假死机【篇】
Redis introduction complete tutorial: client case analysis
Redis getting started complete tutorial: common exceptions on the client
How does C language (string) delete a specified character in a string?