当前位置:网站首页>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
边栏推荐
- Jerry's users how to handle events in the simplest way [chapter]
- Chapter II in memory architecture (im-2.2)
- DBA common SQL statements (3) - cache, undo, index and wait events
- Odoo server setup notes
- Go language practice mode - functional options pattern
- DBA常用SQL语句(3)- cache、undo、索引和等待事件
- DBA常用SQL语句(6)- 日常管理
- 通过流式数据集成实现数据价值(5)- 流处理
- 【无标题】
- NEC infrared remote control coding description
猜你喜欢
MapReduce计算流程详解
杰理之更准确地确定异常地址【篇】
一文看懂 LSTM(Long Short-Term Memory)
Juc并发编程09——Condition实现源码分析
Operation of 2022 tea artist (primary) test question simulation test platform
0704、ansible----01
Jerry's more accurate determination of abnormal address [chapter]
Juc并发编程07——公平锁真的公平吗(源码剖析)
Sim Api User Guide(4)
[untitled]
随机推荐
0704、ansible----01
【无标题】
DBA common SQL statements (5) - latch related
JUC concurrent programming 09 -- source code analysis of condition implementation
正大国际讲解道琼斯工业指数到底是什么?
101. Symmetric Tree
Realizing data value through streaming data integration (5) - stream processing
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
解决方案架构师的小锦囊 - 架构图的 5 种类型
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
SQL tuning series - Introduction to SQL tuning
Juc并发编程09——Condition实现源码分析
Realizing data value through streaming data integration (4) - streaming data pipeline
206、反转链表(链表)
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
杰理之AES能256bit吗【篇】
[untitled]
Using idea to develop Spark Program
Sim Api User Guide(4)