# 2.无头单向非循环链表

**注意：**这种链表结构的头不确定，一直在变

# 3.创建链表

``````//ListNode代表一个节点
class ListNode {

public int val;
public ListNode next;

public ListNode(int val) {

this.val = val;
}
}
``````

5个节点，穷举法创建：

``````public ListNode head;//链表的头引用
public void createList() {

ListNode listNode1 = new ListNode(12);
ListNode listNode2 = new ListNode(23);
ListNode listNode3 = new ListNode(34);
ListNode listNode4 = new ListNode(45);
ListNode listNode5 = new ListNode(56);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
}
``````

# 4.遍历链表

``````public void display() {

while (cur != null) {

System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
``````

``````public class TestDemo {

public static void main(String[] args) {

}
}
``````

# 5.功能列表

## 5.1查找是否包含关键字key是否在单链表当中

``````public boolean contains(int key) {

while (cur != null) {

if (cur.val == key) {

return true;
}
cur = cur.next;
}
return false;
}
``````

``````public class TestDemo {

public static void main(String[] args) {

System.out.println(flg);
}
}
``````

## 5.2得到单链表的长度

``````  //得到单链表的长度
public int size() {

int count = 0;
while (cur != null) {

count++;
cur = cur.next;
}
return count;
}
``````

``````public class TestDemo {

public static void main(String[] args) {

System.out.println(flg);
}
}
``````

## 5.3头插法

``````ListNode node = new ListNode(data);
``````

`````` //头插法

ListNode node = new ListNode(data);
}
``````

``````public class TestDemo {

public static void main(String[] args) {

System.out.println(flg);
}
}
``````

## 5.4尾插法

`````` //尾插法

ListNode node = new ListNode(data);

} else {

while (cur.next != null) {

cur = cur.next;
}
cur.next = node;
}
}
``````

``````public class TestDemo {

public static void main(String[] args) {

}
}
``````

## 5.5任意位置插入,第一个数据节点为0号下标

`````` /**
* @param index
* @Description:找到index-1位置节点的地址
* @return: ListNode
*/
public ListNode findIndex(int index) {

while ((index - 1) != 0) {

cur = cur.next;
index--;
}
return cur;
}
``````
`````` //任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {

if (index < 0 || index > size()) {

System.out.println("index位置不合法！");
return;
}
if (index == 0) {

return;
}
ListNode cur = findIndex(index);
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
``````

## 5.6删除第一次出现关键字为key的节点

`````` /**
* @param key
* @Description:找要删除节点的前驱
* @return: ListNode
*/
public ListNode searchPerv(int key) {

while (cur.next != null) {

if (cur.next.val == key) {

return cur;
}
cur = cur.next;
}
return null;
}

//删除第一次出现关键字为key的节点
public void remove(int key) {

System.out.println("单链表为空,不能删除！");
return;
}

return;
}

ListNode cur = searchPerv(key);
if (cur == null) {

System.out.println("没有要删除的节点！");
return;
}
ListNode del = cur.next;
cur.next = del.next;
}
``````

``````public class TestDemo {

public static void main(String[] args) {

}
}
``````

## 5.7删除所有值为key的节点

``````//删除所有值为key的节点
public ListNode removeAllKey(int key) {

if (this.head == null) return null;

while (cur.next != null) {

if (cur.val == key) {

perv.next = cur.next;
cur = cur.next;
} else {

perv = cur;
cur = cur.next;
}
}

//最后处理头

}
}
``````

``````public class TestDemo {

public static void main(String[] args) {

}
}
``````

## 5.8清空链表

``````   //清空链表
public void clear() {

}
}
``````

``````public class TestDemo {

public static void main(String[] args) {

System.out.println("请空前");
System.out.println("请空后");
System.out.println("==========");
}
}
``````