当前位置:网站首页>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;
}
}
边栏推荐
- OC, OD, push-pull explanation of hardware
- [secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
- Kubernetes source code analysis (II) -- resource
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
- mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA
- Matlab Error (Matrix dimensions must agree)
- 安装 torch 0.4.1
- Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform
- 换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!
- [tools] basic concept of database and MySQL installation
猜你喜欢
Flink Task退出流程与Failover机制
IDEA重启后无法创建Servlet文件的解决方案
Lavel PHP artisan automatically generates a complete set of model+migrate+controller commands
Use of promise in ES6
QT Bluetooth: qbluetooth DeviceInfo
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
Make (convert) ICO Icon
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
centerX: 用中国特色社会主义的方式打开centernet
随机推荐
SQL中删除数据
Redis入门完整教程:客户端管理
Redis getting started complete tutorial: common exceptions on the client
Jerry's question about DAC output power [chapter]
Redis Getting started tutoriel complet: positionnement et optimisation des problèmes
mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA
C language string sorting
Netperf and network performance measurement
Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
IDEA重启后无法创建Servlet文件的解决方案
Summary of research status of inertial navigation calibration at home and abroad (abridged version)
【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application
Room rate system - login optimization
How does C language (string) delete a specified character in a string?
应用程序启动速度的优化
Shell 编程基础
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
leetcode
Redis introduction complete tutorial: replication principle