当前位置:网站首页>两两交换链表中的节点[单向链表不断链原则]
两两交换链表中的节点[单向链表不断链原则]
2022-06-22 11:10:00 【REN_林森】
单向链表不断链原则
前言
对于单向链表操作的基本功之一,就是不断链。单向链表不断链原则,把后面被动的节点先接上。再操作用指针指到的节点,反正有指针指,不会丢失。
一、两两交换链表中的节点

二、不断链
package everyday;
// 两两交换链表中的节点
public class SwapPairs {
/* target:把链表相邻偶数奇数节点交换。 在这里,主要就涉及链表断链问题,如果能拿到两节点的前一个和后一个就不成问题。 为了统一第一个节点的操作,可加上dummyHead; */
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode dummyHead = new ListNode(0, head);
ListNode p = dummyHead, q = dummyHead.next;
while (q != null && q.next != null) {
// q记住第一个节点去,p记住q前面的节点,先把q后面的链表接到p后,再把q嵌入进去(毕竟单向链表,先把后面的接上来)。
// 注:单向链表不断链原则,把后面被动的节点先接上。再操作用指针指到的节点,反正有指针指,不会丢失。
p.next = q.next;
q.next = p.next.next;
p.next.next = q;
// 更新pq
p = q;
q = q.next;
}
return dummyHead.next;
}
// 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;
}
}
}
总结
1)单向链表不断链原则,把后面被动的节点先接上。再操作用指针指到的节点,反正有指针指,不会丢失。
参考文献
边栏推荐
- How to improve customer conversion rate on the official website
- CVPR 2022 Oral | 以运动为导向的点云单目标跟踪新范式
- [shell] collection of common instructions
- Should the theme of the IDE be bright or dark? Here comes the ultimate answer!
- Daily question 5-1636 Sort arrays in ascending order by frequency
- 奋斗吧,程序员——第三十六章 落花人独立,微雨燕双飞
- R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、使用lm函数对匹配后的样本构建线性回归模型、summary函数查看模型的汇总统计信息
- Interpretation of MAML (model agnostic meta learning)
- ARM加载存储指令
- R language performs two sample t-test on the specified covariates based on the with function, and the t.test function performs Welch two sample t-test analysis and two independent sample t-test on the
猜你喜欢

Wechat applet project example - image processing gadget (self-made low configuration version of Meitu XiuXiu)

How many of the eight classic MySQL errors did you encounter?

【软工】 软件体系结构

Basic principles of the Internet

机器人强化学习——Sim-to-Real Robot Learning from Pixels with Progressive Nets (2017)

Exchange sort method

庖丁解牛,这八个MySQL经典错误,你遇到几个?

如果你是个半路出家的程序员,请一字一句的看完

Convert the colored liquid image into transparent liquid, and CMU teaches the robot to accurately control how much water is poured into the cup

Getting to know elastricearch
随机推荐
From prototype chain to inheritance, illustrate the context and recommend collection
Pychart debugging is stuck and connected appears
6-9 inter application communication - sub application communication
6-13 improving load performance - application cache
CF751E Phys Ed Online
7-1 framework Publishing - publishing framework through NPM
jg_ Using easyexcel to read Excel_ twenty million two hundred and twenty thousand six hundred and nineteen
【软工】 设计模块
R language uses user-defined functions to write step activation functions for deep learning and visualize step activation functions
牛客挑战赛57C题解
Basic principles of the Internet
In depth analysis of business model of blind box software development in 2022
[Software Engineering] Introduction & process and life cycle modeling
TCP 3次握手的通俗理解
【软工】 软件体系结构
6-10 全局状态管理 - 全局store
Interpretation of MAML (model agnostic meta learning)
ARM加载存储指令
Daily question 5-1636 Sort arrays in ascending order by frequency
R language performs two sample t-test on the specified covariates based on the with function, and the t.test function performs Welch two sample t-test analysis and two independent sample t-test on the