Create SinglyLinkedList.java
parent
fd72f740eb
commit
c6b68cd40e
|
|
@ -0,0 +1,39 @@
|
|||
package com.nbee.solution.practice;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 链表类
|
||||
*/
|
||||
public class SinglyLinkedList {
|
||||
private Node head;
|
||||
private static class Node {
|
||||
int value;
|
||||
Node next;
|
||||
|
||||
Node(int value, Node next) {
|
||||
this.value = value;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
public void addFirst(int value) {
|
||||
//链表为空
|
||||
// head = new Node(value,null);
|
||||
//链表非空,包含了链表为空的情况
|
||||
head = new Node(value,head);
|
||||
}
|
||||
public void loop(Consumer<Integer> consumer) {
|
||||
Node p = head;
|
||||
while (p != null) {
|
||||
consumer.accept(p.value);
|
||||
p = p.next;
|
||||
}
|
||||
}
|
||||
|
||||
public void loop0 (Consumer<Integer> consumer){
|
||||
for (Node p = head; p != null; p = p.next) {
|
||||
consumer.accept(p.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue