当前位置: 首页 > news >正文

网站建设前置审批网站配色主题

网站建设前置审批,网站配色主题,青岛营销型网站制作,怎么自己创建一个网站代码题目来源 24. 两两交换链表中的节点 - 力扣(LeetCode) 题目描述 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换&…

题目来源

24. 两两交换链表中的节点 - 力扣(LeetCode)

题目描述

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例

示例 1:

输入:head = [1,2,3,4]
输出:[2,1,4,3]

示例 2:

输入:head = []
输出:[]

示例 3:

输入:head = [1]
输出:[1]

提示

  • 链表中节点的数目在范围 [0, 100] 内
  • 0 <= Node.val <= 100

题目解析

本题主要考察数据结构。

对于输入的链表,我们可以为其定义一个虚拟头节点 dummy_head,比如示例1,进行如下逻辑

C源码实现

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* swapPairs(struct ListNode* head) {struct ListNode* dummy_head = (struct ListNode*)malloc(sizeof(struct ListNode));dummy_head->val = 0;dummy_head->next = head;struct ListNode* pre = dummy_head;struct ListNode* cur = pre->next;while (cur != NULL && cur->next != NULL) { // 由于要交换cur和cur.next两个节点,因此二者不能为nullstruct ListNode* nxt = cur->next;cur->next = nxt->next;nxt->next = cur;pre->next = nxt;pre = cur;cur = pre->next;}return dummy_head->next;
}

C++源码实现

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* swapPairs(ListNode* head) {ListNode* dummy_head = new ListNode(0, head);ListNode* pre = dummy_head;ListNode* cur = pre->next;while (cur != nullptr && cur->next != nullptr) { // 由于要交换cur和cur.next两个节点,因此二者不能为nullListNode* nxt = cur->next;cur->next = nxt->next;nxt->next = cur;pre->next = nxt;pre = cur;cur = pre->next;}return dummy_head->next;}
};

Java源码实现

/*** 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 swapPairs(ListNode head) {ListNode dummy_head = new ListNode(0, head);ListNode pre = dummy_head;ListNode cur = pre.next;while (cur != null && cur.next != null) { // 由于要交换cur和cur.next两个节点,因此二者不能为nullListNode nxt = cur.next;cur.next = nxt.next;nxt.next = cur;pre.next = nxt;pre = cur;cur = pre.next;}return dummy_head.next;}
}

Python源码实现

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):def swapPairs(self, head):""":type head: Optional[ListNode]:rtype: Optional[ListNode]"""dummy_head = ListNode(0, head)pre = dummy_headcur = pre.nextwhile cur and cur.next:  # 由于要交换cur和cur.next两个节点,因此二者不能为nullnxt = cur.nextcur.next = nxt.nextnxt.next = curpre.next = nxtpre = curcur = pre.nextreturn dummy_head.next

JavaScript源码实现

/*** Definition for singly-linked list.* function ListNode(val, next) {*     this.val = (val===undefined ? 0 : val)*     this.next = (next===undefined ? null : next)* }*/
/*** @param {ListNode} head* @return {ListNode}*/
var swapPairs = function (head) {const dummy_head = new ListNode(0, head);let pre = dummy_head;let cur = pre.next;while (cur != null && cur.next != null) { // 由于要交换cur和cur.next两个节点,因此二者不能为nullconst nxt = cur.next;cur.next = nxt.next;nxt.next = cur;pre.next = nxt;pre = cur;cur = pre.next;}return dummy_head.next;
};

http://www.dtcms.com/a/529067.html

相关文章:

  • 服务器pdb断点调试
  • 一次方的高阶无穷小 = 一次方 × 无穷小
  • 网站建设请款报告广东建设工程信息网官网首页
  • 【力扣hot100】238.除自身以外数组的乘积
  • 江门网站关键词推广seo关键词优化软件合作
  • 如何解决 pip install 安装报错 Backend ‘setuptools.build_meta’ 不可用 问题
  • C++智能指针使用指南(auto_ptr, unique_ptr, shared_ptr, weak_ptr)
  • GitHub等平台形成的开源文化正在重塑知可以谈人
  • 计算机网络:知识点梳理及讲解(三)数据链路层
  • PySide6 pyside6-deploy 命令 Linux 部署
  • 网站内的搜索怎么做的学校网站建设情况报告
  • 电脑建立网站朝阳区手机网站建设服务
  • 谷歌商店下载APK教程,先下载谷歌三件套,再直接从 Google Play 下载 APK 文件?
  • Spring中事务的传播行为
  • 将镜像推送到 Docker Hub 或私有仓库
  • 做qq图片的网站吗wordpress没有链接地址
  • 面向模块的综合技术之重定时优化(六)
  • 社交模板网站建设成都青羊网站建设
  • Android 数据持久化(SharedPreferences)
  • 四、高效注意力机制与模型架构
  • 沧州做网站公司兰州网站建站
  • C++ STL:string类(1) |了解string|编码|常用接口|迭代器|算法查找|auto|范围for
  • ESP32 Linux 开发环境
  • 网站建设全国排名alexa排名前三十
  • 声乐基础知识学习
  • Redis底层原理-持久化【详细易懂】
  • 现在建网站可以拖拉式的吗深圳住房建设网站
  • 流量打不开网站怎么办营销型企业网站 网络服务
  • 如何用ps做网站ui如何在云主机上建设网站
  • 营销网站建设818gx做商城网站多少钱