当前位置:网站首页>24. Exchange the nodes in the linked list (linked list)
24. Exchange the nodes in the linked list (linked list)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Given a linked list , Two or two exchange the adjacent nodes , And return the list after exchange .
You can't just change the values inside the node , It needs to actually exchange nodes .
public class Liangliangjiaohuanlianbiaozhongdejiedian {
static class ListNode{
int val;// data : Node data
ListNode next;// object : Object of node
ListNode(int val){
this.val = val;
}
}
public ListNode swapPairs(ListNode head){
// Set up a virtual head node
ListNode dummy = new ListNode(0);
dummy.next = head;
// Define the current node
ListNode cur = dummy;
while (cur.next!=null && cur.next.next!=null){
// Define temporary nodes
ListNode temp = cur.next;
ListNode temp1= cur.next.next.next;
// Step one
cur.next = cur.next.next;
// Step two
cur.next.next = temp;
// Step three
cur.next.next.next = temp1;
//cur Move two bits for the next round of exchange
cur = cur.next.next;
}
return dummy.next;
}
// Printout
static void print(ListNode listNode){
// Create a linked list node
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) {
// Create the first node
ListNode nodeSta = new ListNode(1);
// Declare a variable to represent the current node during the move
ListNode nextnode;
nextnode = nodeSta;
for (int i = 2; i <=6 ; i++) {
// Generate new nodes
ListNode node = new ListNode(i);
// The mobile node points to the new node ( Connect the new node
nextnode.next = node;
// Move the current node back
nextnode = nextnode.next;
}
// Linked list generation completed
// Print directly from the first node
print(nodeSta);
System.out.println();
Liangliangjiaohuanlianbiaozhongdejiedian liangliangjiaohuanlianbiaozhongdejiedian = new Liangliangjiaohuanlianbiaozhongdejiedian();
ListNode res = liangliangjiaohuanlianbiaozhongdejiedian.swapPairs(nodeSta);
print(res);
}
}
Time complexity :O(n)
Spatial complexity :O(1)
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011141032.html
边栏推荐
- "Gu Yu series" airdrop
- DBA常用SQL语句 (5) - Latch 相关
- 206、反转链表(链表)
- Using multithreading to output abc10 times in sequence
- 深度选择器
- Realizing data value through streaming data integration (4) - streaming data pipeline
- 454、四数之和(哈希表)
- Realizing data value through streaming data integration (5) - stream processing
- DBA常用SQL语句(3)- cache、undo、索引和等待事件
- Ansible cloud computing automation command line compact version
猜你喜欢

JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer

深度选择器

Depth selector

2022茶艺师(初级)考试试题模拟考试平台操作

Computer network security experiment II DNS protocol vulnerability utilization experiment

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

lnmp的配置

2022年上海市安全员C证考试题库及答案

JVM——》常用参数

Sim Api User Guide(5)
随机推荐
Function realization of printing page
DBA common SQL statements (2) - SGA and PGA
Chapter II in memory architecture (im-2.2)
域名和IP地址的联系
Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Sim Api User Guide(4)
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
DBA common SQL statements (5) - latch related
DBA common SQL statements (3) - cache, undo, index and wait events
Realizing data value through streaming data integration (5) - stream processing
Solution architect's small bag - 5 types of architecture diagrams
art-template 模板引擎
MapReduce压缩
1、两数之和(哈希表)
DBA common SQL statements (1) - overview information
Realize data value through streaming data integration (3) - real-time continuous data collection
LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
Six practices of Windows operating system security attack and defense
利用多线程按顺序连续输出abc10次