LeetCode刷题笔录Swap Nodes in Pairs

台达PLC使用RS485Modbus指令通信 台达PLC,RS485,Modbus RTU,从站梯形图,0X01,0X02,0X05,CRC校验 阅读详情

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.


这题想法和Merge Two Sorted Linked List类似,也是建一个fakeHead,然后一个一个把原来链表里的node插到新链表里。

比如这个1->2,p1=1。那么dummyP.next=2, nextHead=p1.next.next. dummyP.next.next = 1.最后p1.next=null。一定要记住最后一步,要不然新链表就是2121212121....没完没了了

public class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode fakeHead = new ListNode(0);
        ListNode dummyP = fakeHead;
        ListNode p1 = head;
        ListNode nextHead;
        while(p1 != null){
            if(p1.next == null){
                dummyP.next = p1;
                break;
            }
            nextHead = p1.next.next;
            dummyP.next = p1.next;
            p1.next.next = p1;
            //must set p1.next = null to avoid self loops
            p1.next = null;
            p1 = nextHead;
            dummyP = dummyP.next.next;
        }
        return fakeHead.next;
    }
}


深度学习模型设计的方法和技巧 设计神经网络模型是一个迭代的过程,需要不断试验和调整各个组成部分,以达到最佳性能。同时,遵循良好的工程实践,如模块化设计、版本控制、文档记录等,也是成功的关键。 阅读详情

相关推荐

腾讯云服务器被攻击,带宽被占满的抗战日记

故事 时间:2021-08-23 地点:腾讯云服务器 事件:服务器内容被黑,修复后某人气不过,开始DDOS,占满服务器的带宽,导致网站访问非常缓慢。 杂记 根据腾讯云后台+宝塔后台,带宽被占满:(小垃圾水管没办法) 部分参考:记一次阿里云服务器ECS流出带宽占满的解决过程 https://blog.youkuaiyun.com/wangzhihao1994/article/details/99563456 首先是在云主机上使用远程连接访问到云主机,安装iftop工具 CentOS .

Rudon滨海渔村的博客 1303

LeetCode刷题笔录 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo

likecool21的专栏 1万+

QGIS-空间分析-核密度分析

本文在QGIS软件中使用核密度估计法分析全球历年的地震数据,旨在分析出地震频发且严重的区域,为地震防控提供参考依据。全球历年地震数据来源于网络,仅用于技术学习探索。通过核密度分析,可以观察到南美、美国西部海岸线和太平洋北部区域在全球地震分布状态中是最为集中和强烈的。

imxiezy的博客 2467

LeetCode刷题笔录 Two Sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the

likecool21的专栏 1万+

【医学影像】超声(UltraSound)影像的增强与目标检测

文章目录1 综述1.1 超声发展简史1.2 超声类型2 超声影像的去噪与增强2.1 各向异性扩散去噪模型2.2 色彩恢复的视网膜增强算法SSR和MSR2.3 MSRCR增强算法(基于MSR算法改进版)2.4 参考文献3 超声影像的目标识别3.1 基于跨模态数据迁移进行DL训练3.2 参考文献 1 综述 因工作需要,最近调研了超声(UltraSound)影像的相关知识,包含超声类型、超声影像的去噪和增强,目标检测等,并对相关资料文献进行梳理整合。 本人另外一篇博文主要讲了UltraSound 影像与CT/MR

muyijames的博客 1万+

基于Mind+、MOTT实现人工智能

@基于Mind+、MOTT实现人工智能 人工智能实验 一、实验目的及要求 【一】实现人脸识别 【二】实现云数据库应用 【三】实现测温数据mqtt上传至物联网云平台 二、实验原理与内容 基于二哈实现多人人脸学习和识别 基于TinywebDB实现云数据库应用 基于EasyIoT实现mqtt数据上传。 三、实验软硬件环境 硬件:掌控板 软件:Mind+ 四、实验过程(实验步骤、记录、数据、分析) 1.基于二哈实现多人人脸学习和识别 ①在Mind+扩展上的传感器选项选择HUSKYLENS AI 摄像头 ②配置哈士

weixin_44606930的博客 2374

LeetCode刷题笔录 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.Th

likecool21的专栏 6928

Linux内存压力测试工具【memtester】详且精

对Linux下的压力测试工具memtester有一个比较人性化的概述,以及避坑心得。

weixin_44118923的博客 1万+

Stanford Algorithms学习:Clustering 2

这是紧接上面的一道题,比较有趣 Question 2 In this question your task is again to run the clustering algorithm from lecture, but on a MUCH bigger graph. So big, in fact, that the distances (i.e., edge costs

likecool21的专栏 2567

Python电梯II

输入数据有若干组(最多不超过1000组),每组数据的第一行为四个如题目中所示的整数N,u,d,s,由空格分隔开。第二行为不同楼层需要上下楼的请求, 最多不超过1000个。每个请求的格式为"from-to",其中"from"和"to"为所在楼层和要到达的楼层,保证from!=to。

沐风—云端行者 909

SAP自动化-ME51N批量创建采购申请

2、需要管理员授权RZ11脚本录制回放功能。1、仅限于正对SAP日常工作的批量处理。3、数据模板按实际需求更新。物料编码,工厂,数量。

小九啥也不会 530

Stanford Algorithms学习:Clustering

第二周的第一个编程作业,是利用贪婪算法来实现一个clustering的问题,和ML里学的unsupervised learning差不多。 Question 1 In this programming problem and the next you'll code up the clustering algorithm from lecture for computing a

likecool21的专栏 2507

【异常】SpringCloud Gateway报错503 SERVICE_UNAVAILABLE “Unable to find instance for xxx-gen“

网关服务报错,如下。

奶爸的编程之路,也就一周冷个三天 4223

LeetCode 刷题笔录 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link

likecool21的专栏 1806

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LeetCode刷题笔录Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word:

likecool21的专栏 1296

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

LeetCode刷题笔录Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

likecool21的专栏 1109

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode刷题笔录 Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题比较简单

likecool21的专栏 1043

LeetCode刷题笔录Unique Binary Search Trees

这里有个讲解(印度人口音真难懂)

likecool21的专栏 1043

LeetCode刷题笔录Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin

likecool21的专栏 1001

LeetCode刷题笔录 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 随手写的一个,正常的读出每一位的数字然后反序: import java.lang.Math; public class Solution { public int revers

likecool21的专栏 941

LeetCode刷题笔录Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

likecool21的专栏 878

LeetCode刷题笔录Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

likecool21的专栏 865

LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai

likecool21的专栏 855

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 365

LeetCode 每日一题 2026/11/2-2026/11/8

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步

AlphaTao的博客 176

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 208
上一篇: LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree
下一篇: LeetCode刷题笔录Symmetric Tree
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值