目录
1.创建节点类
- val--链表节点存放的数据,这里我们使用int数据类型.
- next--存放下一个节点的地址
class ListNode{
public int val;
public ListNode next;
//构造函数
public ListNode(int =num){
this.val = num;
}
2.创建链表
方法一:枚举法
public class MyLinkedList {
public ListNode head;//链表的头
public void creatList(){
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
ListNode listNode4 = new ListNode(4);
ListNode listNode5 = new ListNode(5);
this.head = listNode1;
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
}
方法二:头插法public void addFirst(int data)
链表的头节点的位置插入一个新节点,新节点的next指向head,并让head指向next.
public class MyLinkedList {
public ListNode head;//链表的头
public void addFirst(int data){
ListNode node = new ListNode(data);
node.next = this.head;
this.head = node;
}
}
方法三:尾插法public void addLast(int data)
尾节点的位置插入一个新节点,先遍历链表找到尾部的节点,让尾部的节点的next指向新节点.
public void addLast(int data){
ListNode node = new ListNode(data);
if(this.head == null){
this.head = node;
}else {
ListNode cur = this.head;
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
}
3.链表的长度
定义一个int类型变量count来计数,遍历链表,有一个节点,count就++. 最后返回count.
public int size(){
int count = 0;
ListNode cur = this.head;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
4.链表是否包含关键字key
遍历链表,比较节点与key的值是否相等. 相等就返回true,不相等就返回false.
public boolean contains(int key){
ListNode cur = this.head;
while(cur != null){
if(cur.val == key){
return true;
}
cur = cur.next;
}
return false;
}
5.在链表的任意的位置插入节点
当位置==0的时候就是头插法(之前写过的,在标签2.创建链表),位置==长度就是尾插法(之前也写过),我们不用再写一遍,直接调用之前的,
public void addIndex(int index,int data){
if(index < 0 ||index > Size()){ //对index位置的合法性进行判断
return;
}
if(index == 0){ //相当于头插法
addFirst(data);
return;
}
if(index = Size()){ //相当于尾插法
addLast(data);
return;
}
ListNode cur = head.next;
while (index - 1 != 0) {
cur = cur.next;
index--;
}//找到index位置前一位置的地址
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
6.删除第一次出现值为key的节点
如果删除的是头节点,那就让头节点=头节点的next,如果不是,那就需要找到删除节点的前一个节点,
让它的next等于删除节点的next,这样就删除next了.
//找到key的前驱(前一节点)
public ListNode findPre(int key){
ListNode cur = this.head;
while(cur.next != null){
if(cur.next.val == key){
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head == null){
return;
}
if(this.head.val == key){
this.head = this.head.next;
return;
}
ListNode cur = findPre(key);
if(cur == null){
return; //没有要删除的节点
}
ListNode del = cur.next;//定义要删除的节点
cur.next = del.next;
}
7.删除所有值为key的节点
使用双指针,pre是cur的前指针,cur用来判断是否等于key,等于那就让pre.next=cur.next,删掉cur指向的节点,然后继续往下走直到末尾.
public ListNode removeAllKey(int key){
if(this.head = null){
return null;
}
//方便删除头节点
ListNode dummyNode=new ListNode(-1);
dummyNode.next=this.head;
ListNode prev = dummyNode;
ListNode cur = this.head;
while(cur != null){
if(cur.val == key){
prev.next = cur.next;
cur = cur.next;
}else {
prev = cur;
cur = cur.next;
}
}
return dummyNode.next;
}
8.清空所有链表
头节点指向null
public void clear(){
this.head=null;
}
}
9.综合代码
import java.util.List;
//ListNode代表一个节点
class ListNode{
public int val;
public ListNode next;
//构造函数
public ListNode(int num){
this.val = num;
}
}
public class MyLinkedList {
public ListNode head;//链表的头
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
node.next = this.head;
this.head = node;
}
//尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
if (this.head == null) {
this.head = node;
} else {
ListNode cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//得到单链表的长度
public int size() {
int count = 0;
ListNode cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
if (index < 0 || index > Size()) {
return;
}
if (index == 0) { //相当于头插法
addFirst(data);
return;
}
if (index == Size()) { //相当于尾插法
addLast(data);
return;
}
ListNode cur = head.next;
while (index - 1 != 0) {
cur = cur.next;
index--;
}//找到index位置前一位置的地址
ListNode node = new ListNode(data);//初始化node
node.next = cur.next;
cur.next = node;
}
//找到key的前驱(前一节点)
public ListNode fingPre(int key) {
ListNode cur = this.head;
while (cur.next != null) {
if (cur.next.val == key) {
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if (this.head == null) {
return;
}
if (this.head.val == key) {
this.head = this.head.next;
return;
}
ListNode cur = findPre(key);
if (cur == null) {
return; //没有要删除的节点
}
ListNode del = cur.next;//定义要删除的节点
cur.next = del.next;
}
//删除所有值为key的节点
public ListNode removeAllKey(int key) {
if (this.head = null) {
return null;
}
ListNode prev = this.head;
ListNode cur = this.head.next;
while (cur != null) {
if (cur.val == key) {
prev.next = cur.next;
cur = cur.next;
} else {
prev = cur;
cur = cur.next;
}
}
if (this.head.val == key) {
this.head = this.head.next;
}
return this.head;
}
//清空链表
public void clear() {
this.head=null;
}
}