当前位置:网站首页>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;
}
}
边栏推荐
- Kubernetes源码分析(二)----资源Resource
- [tools] basic concept of database and MySQL installation
- DOMContentLoaded和window.onload
- 新标杆!智慧化社会治理
- C language exercises_ one
- Redis getting started complete tutorial: client management
- 制作(转换)ico图标
- IDEA重启后无法创建Servlet文件的解决方案
- Construction of knowledge map of mall commodities
- 杰理之开 BLE 退出蓝牙模式卡机问题【篇】
猜你喜欢
随机推荐
2022.6.28
Leetcode 77: combination
PSINS中19维组合导航模块sinsgps详解(时间同步部分)
杰理之开 BLE 退出蓝牙模式卡机问题【篇】
A complete tutorial for getting started with redis: problem location and optimization
The whole process of knowledge map construction
C language exercises_ one
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (initial assignment part)
尚硅谷JVM-第一章 类加载子系统
Unity uses maskablegraphic to draw a line with an arrow
密码学系列之:在线证书状态协议OCSP详解
惯导标定国内外研究现状小结(删减版)
商城商品的知识图谱构建
Simple bubble sort
Andrews - multimedia programming
应用程序启动速度的优化
Redis introduction complete tutorial: replication principle
Unity使用MaskableGraphic画一条带箭头的线
【达梦数据库】备份恢复后要执行两个sql语句
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅








![[socket] ① overview of socket technology](/img/91/dccbf27a17418ea632c343551bccc0.png)
