当前位置:网站首页>24、两两交换链表中的节点(链表)
24、两两交换链表中的节点(链表)
2022-04-23 10:11:00 【Popuessing's Jersey】
题目:
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
public class Liangliangjiaohuanlianbiaozhongdejiedian {
static class ListNode{
int val;//数据:节点的数据
ListNode next;//对象:节点的对象
ListNode(int val){
this.val = val;
}
}
public ListNode swapPairs(ListNode head){
//设置一个虚拟头节点
ListNode dummy = new ListNode(0);
dummy.next = head;
//定义当前节点
ListNode cur = dummy;
while (cur.next!=null && cur.next.next!=null){
//定义临时节点
ListNode temp = cur.next;
ListNode temp1= cur.next.next.next;
//步骤一
cur.next = cur.next.next;
//步骤二
cur.next.next = temp;
//步骤三
cur.next.next.next = temp1;
//cur移动两位进行下一轮交换
cur = cur.next.next;
}
return dummy.next;
}
//打印输出
static void print(ListNode listNode){
//创建链表节点
while (listNode!=null){
if(listNode.next==null){
System.out.print(listNode.val);
}else {
System.out.print(listNode.val + "->");
}
listNode = listNode.next;
}
}
public static void main(String[] args) {
//创建首节点
ListNode nodeSta = new ListNode(1);
//声明一个变量来表示移动过程中的当前节点
ListNode nextnode;
nextnode = nodeSta;
for (int i = 2; i <=6 ; i++) {
//生成新节点
ListNode node = new ListNode(i);
//移动节点指向新节点(连接新节点
nextnode.next = node;
//把当前节点往后移动
nextnode = nextnode.next;
}
//链表生成完毕
//直接从首节点打印
print(nodeSta);
System.out.println();
Liangliangjiaohuanlianbiaozhongdejiedian liangliangjiaohuanlianbiaozhongdejiedian = new Liangliangjiaohuanlianbiaozhongdejiedian();
ListNode res = liangliangjiaohuanlianbiaozhongdejiedian.swapPairs(nodeSta);
print(res);
}
}
时间复杂度:O(n)
空间复杂度:O(1)
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121410596
边栏推荐
- 杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
- [2020wc Day2] F. Clarice picking mushrooms (subtree and query, light and heavy son thought)
- [hdu6833] a very easy math problem
- DBA常用SQL语句(6)- 日常管理
- [untitled]
- Go language practice mode - functional options pattern
- Sim Api User Guide(6)
- 通过流式数据集成实现数据价值(5)- 流分析
- Sim Api User Guide(7)
- Operation of 2022 tea artist (primary) test question simulation test platform
猜你喜欢

Redis design and Implementation

Question bank and answers of Shanghai safety officer C certificate examination in 2022

IDEA——》每次启动都会Indexing或 scanning files to index

一文看懂 LSTM(Long Short-Term Memory)

Sim Api User Guide(4)

从知识传播的维度对比分析元宇宙

Zhengda international explains what the Dow Jones industrial index is?

Juc并发编程07——公平锁真的公平吗(源码剖析)
![[untitled]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[untitled]

Windows安装redis并将redis设置成服务开机自启
随机推荐
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
2022 mobile crane driver test question bank simulation test platform operation
通过流式数据集成实现数据价值(3)- 实时持续数据收集
打印页面的功能实现
杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
Rain produces hundreds of valleys, and all things grow
Longest common front string
Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
杰理之栈溢出 stackoverflow 怎么办?【篇】
DBA常用SQL语句(4)- Top SQL
杰理之更准确地确定异常地址【篇】
DBA common SQL statements (1) - overview information
ARM调试(1):两种在keil中实现printf重定向到串口的方法
Zhengda international explains what the Dow Jones industrial index is?
Understand scope
1D / 1D dynamic programming learning summary
Mobius inversion
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
Read LSTM (long short term memory)
Turn: Maugham: reading is a portable refuge