当前位置:网站首页>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 .
边栏推荐
- Technology sharing | how to simulate real use scenarios? Mock technology to help you
- Redis implementation of distributed lock and analysis of the main process of redismission distributed lock
- PKU-2739-Sum of Consecutive Prime Numbers(筛素数法打表)
- NiO example
- Graphic pipeline foundation (II)
- Scratch command
- NFS shared storage service
- VMware Workstation 配置net模式
- QT uses MSVC compiler to output Chinese garbled code
- 单项链表的创建、遍历以及按要求查找结点
猜你喜欢

SSH服务配置

mongo ssl 配置实战

Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones

DHCP原理与配置

CentOS7部署MySQL数据库服务器

Which is the best air conduction Bluetooth headset? Air conduction Bluetooth headset ranking

SSAO by computer shader (II)

Ten thousand words summarize and realize the commonly used sorting and performance comparison

技术分享 | 接口测试价值与体系

Mongo SSL configuration practice
随机推荐
[C language] custom structure type
CentOS7部署MySQL数据库服务器
Which brand of air conduction earphones is better? These four should not be missed
Yapi vulnerability hanging horse program chongfu.sh processing
Project compilation nosuch*** error problem
NiO example
[c language] - step by step to achieve minesweeping games
Test interview questions collection (III) | computer network and database (with answers)
网络——网络层
HDU-5805-NanoApe Loves Sequence(思维题)
Graphic pipeline foundation (I)
It is recommended to wear air conduction earphones, which do not need to wear in ear
Prometheus monitoring Nacos
SSH服务配置
mongoDB复制集及分片集群
Which brand of air conduction earphones is good and highly praised
QT uses MSVC compiler to output Chinese garbled code
Question brushing record ---- reverse the linked list (reverse the whole linked list)
测试面试题集锦(一)| 软件测试常见必考问题与流程篇(附答案)
archery数据库审核平台部署