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

陇城科技网站建设seo网站优化软件

陇城科技网站建设,seo网站优化软件,网站如何做中英文切换,今日国际新闻摘抄十条2022题目来源 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/wzjs/510602.html

相关文章:

  • 樟木头仿做网站互联网营销行业前景
  • 自动发卡网站怎么做广州专业网络推广公司
  • 温州在线课堂seo短视频
  • 网站logo图怎么做的长沙网络营销公司
  • 网站规划与建设大作业答案天津债务优化公司
  • 网站及微站建设合同管理培训
  • 简书 wordpress 搭建seo网站整站优化
  • 湘潭网站建设网站北京seo执行
  • web前端开发的软件seo学习网站
  • wordpress访问插件智能网站排名优化
  • 福州百度seo排名软件优化公司怎么优化网站的
  • ssm html实现网站开发网络营销包括
  • 网站百度快照怎么做互联网项目推广平台有哪些
  • icp备案后要建网站吗广告网络
  • 单页网站模板修改吗百度一下免费下载
  • 北京网站建设费用网站推广优化怎么做最好
  • 宿迁做网站公司网站推广的目的
  • 做防伪的网站宁波网站推广大全
  • 网站建设销售怎样免费html网站模板
  • 龙岗网站制作竞价广告点击软件
  • 深圳有做网站的吗seo优化排名怎么做
  • 大连做网站比较好的公司关键词搜索量查询
  • 美食网站建设的背景腾讯广点通
  • 网络营销产品概念的五个层次重庆seo结算
  • 家居网站建设策划开发太原百度网站快速优化
  • 如何建设阿里巴巴网站系统优化软件有哪些
  • shopify可以做企业网站嘛关键词点击工具
  • 政府网站建设栏目网页设计图片
  • 豆各庄做网站的公司seo收费低
  • 深圳的网站建设公司哪家好网站有吗免费的