当前位置:网站首页>Create, traverse and search nodes as required for single linked list
Create, traverse and search nodes as required for single linked list
2022-07-28 06:53:00 【Xiao Qiao】
Catalog
2. Create a linked list of fixed content
Two 、 Traversing the linked list
Get the length of the linked list
3、 ... and 、 Find nodes as required
1. Find out whether there is a node on the linked list
2. Take the last node of the linked list
3. Take the... Of the linked list N Nodes
4. Take the penultimate of the linked list N Nodes
5. Take the penultimate node of the linked list
One 、 Create a linked list
1. establish Node class
// Use Node Represents a node
public class Node {
public int val;
public Node next=null;
public Node(int val) {
this.val = val;
}
@Override
public String toString() {
return "["+val+"]";
}
}
2. Create a linked list of fixed content
// In this way , Create a linked list of fixed contents
public static Node creatList(){
// Create a node
Node a=new Node(1);
Node b=new Node(2);
Node c=new Node(3);
Node d=new Node(4);
// Assign values to references
a.next=b;// hold b The address of is assigned to a Of next in
b.next=c;// hold c The address of is assigned to b Of next in
c.next=d;// hold d The address of is assigned to c Of next in
d.next=null;// This line can be left blank ,Node Class has been initialized to null
return a;// Return to header node
}3. Get the header
Node head=creatList();Two 、 Traversing the linked list
Method 1 (for loop )
for (Node cur=head;cur!=null;cur=cur.next){
System.out.println(cur.val);
}Method 2 (while loop )
Node cur=head;
while(cur!=null && cur.next!=null){
cur=cur.next;
System.out.println(cur.val);
}Get the length of the linked list
Just add count++ that will do .
3、 ... and 、 Find nodes as required
1. Find out whether there is a node on the linked list
int tofind=3;// Suppose the value to be found is 3
Node cur=head;
for (;cur!=null;cur=cur.next){ // Traverse
if (cur.val==tofind){ // Judge
break;
}
}
if (cur!=null){
System.out.println(" eureka ");
}else{
System.out.println(" Did not find ");
}
2. Take the last node of the linked list
Node cur=head;
while(cur!=null && cur.next!=null){
cur=cur.next;
}
// Once the cycle is over ,cur It points to the last node of the linked list
System.out.println(cur.val);3. Take the... Of the linked list N Nodes
int N=3; // Suppose you find the 3 Nodes
Node cur=head;
for (int i=1;i<N;i++){
cur=cur.next;
}
System.out.println(cur.val);4. Take the penultimate of the linked list N Nodes
Last but not least N A node is a positive number size+1-N Nodes
size Indicates the length of the list , Put the N Switch to size+1-N, You can find .
5. Take the penultimate node of the linked list
Characteristic is : .next.next If it is empty, it will jump out of the loop
Node cur=head;
while(cur!=null && cur.next!=null && cur.next.next!=null){
cur=cur.next;
}
System.out.println(cur.val);Judge cur!=null && cur.next!=null To prevent null pointer exceptions .
边栏推荐
- VMware Workstation 配置net模式
- prometheus监控nacos
- JS逆向100题——第1题
- 设计测试用例的方法
- [c language] - step by step to achieve minesweeping games
- How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with
- 技术分享 | 接口测试常用代理工具
- 测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
- Mongodb replica set and partitioned cluster
- Implementation of simple address book in [c language]
猜你喜欢

SSH服务配置

Redis cache design and performance optimization

Lancher deployment practice

cocos2d-x 学习笔记——瓦片地图TiledMap

MySQL master-slave

How to simulate the implementation of strcpy library functions

mysql索引优化

Rain Scene Effect (I)

Mongodb replica set and partitioned cluster

Technology sharing | how to simulate real use scenarios? Mock technology to help you
随机推荐
Technology sharing | detailed explanation of actual combat interface test request methods get, post
Hdu-5806-nanoapelovesequence Ⅱ (ruler method)
Question brushing record - linked list
HDU-1097-A hard puzzle(快速幂)
Yapi vulnerability hanging horse program chongfu.sh processing
mongo ssl 配置实战
TCP/IP五层模型
网络——网络层
File operation in C language
Which brand of air conduction earphones is better? These four should not be missed
Analysis of the semaphore source code of AQS
Centos7 deploy MySQL database server
How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with
单元测试框架Jest搭配TypeScript的安装与配置
Hdu-1159-commonsubsequence (LCS longest common subsequence)
How to store floating point data in memory
Ten thousand words summarize and realize the commonly used sorting and performance comparison
Mongodb quick start
C language memcpy library functions and the role of memmove
修复故障扇区