算法村第一关青铜-链表操作

1.单链表

1.1.定义链表节点类
@Data
public class ListNode {
    private int data;
    private ListNode next;
}
1.2.初始化一个链表
  public ListNode initListNode(ListNode head,int[] arr){
        ListNode cur = head;
        for (int i = 1; i < arr.length; i++) {
            ListNode temp = new ListNode(arr[i],null);
            cur.next = temp;
            cur = cur.next;
        }
        cur.next = null;
        return head;
    }
1.3.遍历链表
public void travelListNode(ListNode head){
        while(head != null){
            System.out.print(head.data+" ");
            head = head.next;
        }
    }
1.4.获取链表长度
 public int getLength(ListNode head){
        int length = 0;
        while(head != null){
            length++;
            head = head.next;
        }
        return length;
    }
1.5.链表插入

有多种插入情况:头插,尾插,中间插入。

1.5.1.头插

新节点的next指向头节点head,并且要将head更改为新的头结点。

 public ListNode insertHead(ListNode head,int val){
        if(head == null) throw new RuntimeException("头节点不能为空");
        // 指向头节点
        ListNode newNode = new ListNode(val,head);
        // 修改头节点
        head = newNode;
        return head;
    }
1.5.2.尾插
 public ListNode insertLast(ListNode head,int val){
        if(head == null) throw new RuntimeException("头节点不能为空");
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        ListNode newNode = new ListNode(val,null);
        cur.next = newNode;
        return head;
    }
1.5.3.中间插入
  public ListNode insert(ListNode head,int val,int position){
        if(head == null) throw new RuntimeException("头节点不能为空");
        ListNode cur = head;
        int count = 1;
        while (count < position - 1){
            count++;
            cur = cur.next;
        }
        ListNode newNode = new ListNode(val,cur.next);
        cur.next = newNode;
        return head;
    }
1.6.链表删除

删除同样有多种情况:头,尾,中。

1.6.1.删除头节点
 public ListNode deleteHead(ListNode head){
        head = head.next;
        return head;
    }
1.6.2.删除尾节点
 public ListNode deleteLast(ListNode head){
        ListNode cur = head;
        while (cur.next.next != null){
            cur = cur.next;
        }
        cur.next = null;
        return head;
    }
1.6.2.中间节点
public ListNode delete(ListNode head,int position){
        ListNode cur = head;
        int count = 1;
        while (count < position - 1){
            count++;
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return head;
    }
1.7.整体代码
package com.ljw.javademo.day1;

import org.junit.jupiter.api.Test;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ljw
 * @Date: 2023/07/18/11:53
 * @Description:
 */
public class ListNodeDemo {
    @Test
    public void ListNodeTest(){
        int[] arr = {1,2,3,4,5,6};
        ListNode listNode = initListNode(arr);
        travelListNode(listNode);
        int length = getLength(listNode);
        System.out.println("链表长度为:"+length);

        ListNode newHead = insertHead(listNode, 24);
        travelListNode(newHead);
        System.out.println("链表长度为:"+getLength(newHead));

        ListNode newHead1 = insertLast(newHead, 99);
        travelListNode(newHead1);
        System.out.println("链表长度为:"+getLength(newHead1));

        ListNode newHead2 = insert(newHead, 88,4);
        travelListNode(newHead2);
        System.out.println("链表长度为:"+getLength(newHead2));

        ListNode newHead3 = deleteHead(newHead2);
        travelListNode(newHead3);
        System.out.println("链表长度为:"+getLength(newHead3));

        ListNode newHead4 = deleteLast(newHead3);
        travelListNode(newHead4);
        System.out.println("链表长度为:"+getLength(newHead4));

        ListNode newHead5 = delete(newHead4,3);
        travelListNode(newHead5);
        System.out.println("链表长度为:"+getLength(newHead5));
    }

    public ListNode initListNode(int[] arr){
        ListNode head = null, cur = null;
        for (int i = 0; i < arr.length; i++) {
            ListNode newNode = new ListNode(arr[i],null);
            // 初始化头节点
            if (i == 0) {
                head = newNode;
                cur = newNode;
            } else {
                cur.next = newNode;
                cur = newNode;
            }
        }
        return head;
    }
    public void travelListNode(ListNode head){
        while(head != null){
            System.out.print(head.data+" ");
            head = head.next;
        }
    }

    public int getLength(ListNode head){
        int length = 0;
        while(head != null){
            length++;
            head = head.next;
        }
        return length;
    }
    public ListNode insertHead(ListNode head,int val){
        if(head == null) throw new RuntimeException("头节点不能为空");
        // 指向头节点
        ListNode newNode = new ListNode(val,head);
        // 修改头节点
        head = newNode;
        return head;
    }
    public ListNode insertLast(ListNode head,int val){
        if(head == null) throw new RuntimeException("头节点不能为空");
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        ListNode newNode = new ListNode(val,null);
        cur.next = newNode;
        return head;
    }

    public ListNode insert(ListNode head,int val,int position){
        if(head == null) throw new RuntimeException("头节点不能为空");
        ListNode cur = head;
        int count = 1;
        while (count < position - 1){
            count++;
            cur = cur.next;
        }
        ListNode newNode = new ListNode(val,cur.next);
        cur.next = newNode;
        return head;
    }

    public ListNode deleteHead(ListNode head){
        head = head.next;
        return head;
    }

    public ListNode deleteLast(ListNode head){
        ListNode cur = head;
        while (cur.next.next != null){
            cur = cur.next;
        }
        cur.next = null;
        return head;
    }

    public ListNode delete(ListNode head,int position){
        ListNode cur = head;
        int count = 1;
        while (count < position - 1){
            count++;
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return head;
    }
}

2.双向链表

双向链表跟单链表相比不过是多了一个前驱指针,整体操作上多了一个前驱指针的修改。不再重复写了,思想基本一致!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值