当前位置:网站首页>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 .
边栏推荐
猜你喜欢

mongoDB复制集及分片集群

2022 Tanabata gift recommendation! Nice, cheap and practical gift recommendation

What kind of air conduction Bluetooth headset with good configuration is recommended

QGraphicsView提升为QChartView

Prometheus monitoring Nacos

Using C language to realize three piece chess games

Mongo SSL configuration practice

Graphic pipeline foundation (part outside)

What is the most practical gift for Tanabata? A gift that will never go wrong is worth buying

JS reverse question 100 - question 1
随机推荐
DNS forward resolution experiment
[explain in detail how to realize Sanzi chess step by step]
Qgraphicsview promoted to qchartview
Test interview questions collection (III) | computer network and database (with answers)
遍历 二叉树
Initializingbean interface and examples
Source code analysis of countdownlatch of AQS
VMware Workstation 配置net模式
JS逆向100题——第1题
Technology sharing | common proxy tools for interface testing
redis缓存设计与性能优化
@Postconstruct annotations and useful examples
Fermat's theorem
QT使用MSVC编译器输出中文乱码问题
单项链表的创建、遍历以及按要求查找结点
Hdu-5805-nanoape loves sequence (thinking questions)
Test life | second tier cities with an annual salary of more than 40W? How did you achieve 100% salary increase under the epidemic?
手把手教你三步完成测试监控系统搭建
NiO example
How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with