Github:
https://github.com/345166018/Algorithm/blob/master/HxAlgo/src/linkedlist/SinglyLinkedList.java
1 顺序的单链表
定义结点
/**
* 定义结点
*
*/
public static class Node {
int val;
Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
public int getVal() {
return val;
}
}
定义一个头结点head为null
private Node head = null;
在链表尾部插入结点
/**
* 顺序插入
* 链表尾部插入
* 找到链表的末尾结点,把新添加的数据作为末尾结点的后续结点
*/
public void insertTail(int val) {
Node newNode = new Node(val, null);//新结点的next指针指向null
if (head == null) {//如果头结点为null,直接将新结点作为头结点
head = newNode;
} else {//否则将新结点插入到链表末尾
Node q = head;
while (q.next != null) {
q = q.next;
}
newNode.next = q.next;//其实还是将新结点的next指针指向null(q.next为null)
q.next = newNode;//将新结点插入到链表末尾
}
}
打印所有结点值
/**
* 打印链表的所有结点值
*/
public void printAll() {
Node p = head;
while (p != null) {
System.out.println(p.val + "");
p = p.next;
}
System.out.println();
}
测试
public static void main(String[] args) {
HxSinglyLinkedList link = new HxSinglyLinkedList();
int data[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < data.length; i++) {
link.insertTail(data[i]);
}
link.printAll();
}
打印结果:
1
2
3
4
5
2 逆序的单链表
表头部插入,这种操作将于输入的顺序相反,逆序
/**
* 表头部插入,这种操作将于输入的顺序相反,逆序
*/
public void insertToHead(int value) {
Node newNode = new Node(value, null);
if (head == null) {
head = newNode;
}else {
newNode.next = head;//将新结点的next指针指向头结点,就是在表头部插入
head = newNode;
}
}
贴上所有代码
package linkedlist;
public class SinglyLinkedList {
public static void main(String[] args) {
SinglyLinkedList link = new SinglyLinkedList();
int data[] = { 1, 2, 3, 4, 5 };
Node p = link.createNodeByTail(data);
link.printAll(p);
Node q = link.createNodeByHead(data);
link.printAll(q);
}
public static class Node {
private int data;
private Node next;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
public int getData() {
return data;
}
}
//链表尾部插入
public Node createNodeByTail(int[] data) {
Node head = null;
// 顺序插入
// 链表尾部插入
for (int i = 0; i < data.length; i++) {
Node newNode = new Node(data[i], null);
// 空链表,可以插入新节点作为head,也可以不操作
if (head == null) {
head = newNode;
} else {
Node q = head;
while (q.next != null) {
q = q.next;
}
q.next = newNode;
}
}
return head;
}
//链表头部插入
public Node createNodeByHead(int[] data) {
Node head = null;
// 表头部插入,这种操作将于输入的顺序相反,逆序
for (int i = 0; i < data.length; i++) {
Node newNode = new Node(data[i], null);
if (head == null) {
head = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
return head;
}
public void printAll(Node p) {
while (p != null) {
System.out.println(p.data + "");
p = p.next;
}
System.out.println();
}
private Node head = null;
// 顺序插入
// 链表尾部插入
public void insertTail(int value) {
Node newNode = new Node(value, null);
// 空链表,可以插入新节点作为head,也可以不操作
if (head == null) {
head = newNode;
} else {
Node q = head;
while (q.next != null) {
q = q.next;
}
newNode.next = q.next;
System.out.println("hongx q next data = " + q.next.data);
q.next = newNode;
}
}
/**
* 表头部插入,这种操作将于输入的顺序相反,逆序
*/
public void insertToHead(int value) {
Node newNode = new Node(value, null);
if (head == null) {
head = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
public void printAll() {
Node p = head;
while (p != null) {
System.out.println(p.data + "");
p = p.next;
}
System.out.println();
}
}
Github:
https://github.com/345166018/Algorithm/blob/master/HxAlgo/src/linkedlist/SinglyLinkedList.java