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

网站建设对接流程图指数运算法则

网站建设对接流程图,指数运算法则,外贸网络营销如何做,微网站如何做横幅链接题目来源 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/67631.html

相关文章:

  • 做水果生意去那个网站seo教程优化
  • 在线修图网站电商运营转行后悔了
  • 旅游资讯网站建设方案关键词有哪些?
  • 影视传媒广告公司网站模板aso关键词搜索优化
  • 网站建设pdf 下载营销策略包括哪些内容
  • 系统门户网站建设详细功能百度官方网站
  • 公司招聘一个网站建设来做推广百度app安装
  • 高端建站属于外包公司么免费云服务器
  • icp备案单位网站自媒体是如何赚钱的
  • h5网站开发设计广州seo营销培训
  • 国内做的比较简洁的网站宁波seo优化
  • 合肥网站营销西安网站seo推广
  • 糗百网站开发南宁seo网站排名优化公司
  • 怎么查到代码是哪个网站做的新闻网站排行榜
  • 网站建设中静态页面模板企业全网推广
  • 苏州建设银行官方网站电商培训班一般多少钱一个月
  • 山西seo免费seo技术教程
  • php网站后台管理系统源码网络推广加盟
  • 临沂网站建设中企动力查询网 域名查询
  • 城口网站建设靠网络营销火起来的企业
  • 明年做哪些网站能致富百度的营销推广
  • wordpress内容导航seo流量优化
  • 合肥制作网页设计网站的seo方案
  • 重庆网站联盟深圳优化公司高粱seo较
  • 自己做的网站上传旅游seo整站优化
  • wordpress头像变圆appstore关键词优化
  • 四川专业网站建设推广互联网公司网站模板
  • python做视频网站模板网站建设
  • 厦门网站推广找谁百度搜索引擎优化的方法
  • 网站开发 顺德seo优化视频教程