当前位置:网站首页>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 .
边栏推荐
- [untitled]
- Technology sharing | interface testing value and system
- DNS domain name resolution service
- HDU-5783 Divide the Sequence(贪心水题)
- Initializingbean interface and examples
- Code tidiness (I)
- Mongodb quick start
- How to calculate the size of structure, segment and Consortium (common body)
- 软件开发中常见模型
- 软件测试(概念篇)
猜你喜欢

DNS域名解析服务

SSAO by computer shader (I)

MySQL master-slave

DNS domain name resolution service

Skimming records -- sequence traversal of binary tree

File operation in C language

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

iptables防火墙

mysql索引优化

SSAO by computer shader (II)
随机推荐
Qgraphicsview promoted to qchartview
RayMarching realizes volume light rendering
Ten thousand words summarize and realize the commonly used sorting and performance comparison
HDU-1159-CommonSubsequence(LCS最长公共子序列)
手把手教你三步完成测试监控系统搭建
DNS domain name resolution service
Build php7 private warehouse
技术分享 | 实战详解接口测试请求方式Get、post
MySQL index optimization
DNS域名解析服务
Cocos2d-x learning notes Tile Map tiledmap
Graphic pipeline foundation (part outside)
单项链表的创建、遍历以及按要求查找结点
What kind of air conduction Bluetooth headset with good configuration is recommended
Hdu-2036-reform spring breeze blowing all over the ground (polygon area template)
Scratch command
Hdu-5805-nanoape loves sequence (thinking questions)
Hdu-1097-a hard puzzle (fast power)
How to simulate the implementation of strcpy library functions
技术分享 | 如何模拟真实使用场景?mock 技术来帮你