当前位置:网站首页>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;
}
}
边栏推荐
- 「小样本深度学习图像识别」最新2022综述
- [swift] learning notes (I) -- familiar with basic data types, coding styles, tuples, propositions
- How to find file accessed / created just feed minutes ago
- Redis introduction complete tutorial: client case analysis
- The whole process of knowledge map construction
- Oracle connection pool is not used for a long time, and the connection fails
- 简单冒泡排序
- Hazel engine learning (V)
- 又一百万量子比特!以色列光量子初创公司完成1500万美元融资
- Room rate system - login optimization
猜你喜欢

掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺

Don't you know the relationship between JSP and servlet?

mos管实现主副电源自动切换电路,并且“零”压降,静态电流20uA

【基于 RT-Thread Studio的CPK-RA6M4 开发板环境搭建】

tensorboard的使用

Appx代码签名指南

Es6中Promise的使用

A complete tutorial for getting started with redis: problem location and optimization

左程云 递归+动态规划

The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing
随机推荐
A complete tutorial for getting started with redis: AOF persistence
CVPR 2022 最佳论文候选 | PIP: 6个惯性传感器实现全身动捕和受力估计
杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】
Cglib agent in agent mode
An error in SQL tuning advisor ora-00600: internal error code, arguments: [kesqsmakebindvalue:obj]
Redis入门完整教程:客户端管理
Shell 编程基础
Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
杰理之FM 模式单声道或立体声选择设置【篇】
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
HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路
A complete tutorial for getting started with redis: RDB persistence
C language exercises_ one
Matlab Error (Matrix dimensions must agree)
左程云 递归+动态规划
uniapp的表单验证
uniapp适配问题
opencv环境的搭建,并打开一个本地PC摄像头。
2022 information security engineer examination outline
Install redis from zero