编程导航算法通关村第一关|青铜|链表基础

单链表构造方法

Java

JVM有栈区和堆区

栈区:存引用,就是指向实际对象的地址。。

堆区:存的是创建的对象。

image.png

定义

规范的链表定义

public class ListNode{
    private int data;
    private ListNode next;
    public ListNode(int data){
        this.data=data;
    }
    public int getData(){
        return data;
    }
    public void setData(int data){
        this.data=data;
    }
    public ListNode getNext(){
        return next;
    }
    public void setNext(ListNode next){
        this.next=next;
    }
}

LeetCode算法题中常用

public class ListNode {
    public int val;
    public Node next;
    Node(int x) {
        val = x;
        ext = null;//作用不大,写了更标准
    }
}

遍历

public static int getListLength(Node head){
    int length=0;
    Node node=head;
    while(node!=null){
        length++;
        node=node.next;
    }
    return length;
}

插入

public static Node insertNode(Node head,Node nodeInsert,int position){
    //将节点插入一个空链表
    if(head==null){
        return nodeInsert;
    }
    //越界
    int size=getLength(head);
    if(position>size+1||position<1){
        System.out.println("位置参数越界");
        return head;
    }
    //表头插入
    if(position)==1){
        nodeInsert.next=head;
        head=nodeeInsert;
        return head;
    }
    //其他位置插入
    Node pNode=head;
    int count=1;
    while(count<position-1){
        pNode=pNode.next;
        count++;
    }
    nodeInsert.next=pNode.next;
    pNode.next=nodeinsert;
    return head;    
}

删除

public static Node deleteNode(Node head,int position){
    //排除空链表
    if(head==null){
        return null;
    }
    //越界
    int size=getLength(head);
    if(position >size+1||position <1){
        System.out.println("位置参数越界");
        return head;
    } 
    //删除节点
    if(position==1){
        head=head.next;
    }else{
        Node pNode=head;
        int count =1;
        while(count<position-1){
            count++;
            pNode=pNode.next;
        }
        Node delNode=pNode.next;
        pNode.next=delNode.next;
    }
    return head;
}

双向链表

image-20230717133032967

Java

结点

class DoubleNode{
    public int data;
    public DoubleNode pre;
    public DoubleNode next;
    public DoubleNode(int data){
        this.data=data;
    }
}

结构&遍历

public class DoubleLinkList{
    private DoubleNode first;
    private DoubleNode last;
    public DoubleLinkList(){
        first=null;
        last=first;
    }
    //从头遍历
    public void displayForward(){
        DoubleNode current=first;
        while(current!=null){
            current.displayNode();
            current=current.next;
        }
    }
    //从尾部遍历
    public void displayBackward(){
        DoubleNode current=last;
        while(current!=null){
            current.displayNode();
            current=current.pre;
        }
    }
}

插入

从头插入

DoublyLinkList doublyLinkList = new DoublyLinkList();
doublyLinkList.insertFirst(20);
public void insertFirst(int data) {
    DoubleNode newDoubleNode = new DoubleNode(data);
    if (isEmpty()) {
        last=newDoubleNode;//列表初始化时first,last都为null
    }else{
        first.pre=newDoubleNode;
        newDoubleNode.next=first;
    }	
    first=newDoubleNode;//让该结点称为第一个节点
}

从尾插入

public void insertLast(int data) {
    DoubleNode newDoubleNode = new DoubleNode(data);
    if (isEmpty()) {
        first=newDoubleNode;//列表初始化时first,last都为null
    }else{
        newDoubleNode.pre=last;
        last.next=newDoubleNode;
    }
    last=newDoubleNode;//让该结点称为最后一个节点
}

从某个值为key的节点后面插入

public void insertAfter(int key, int data){
    DoubleNode newDoubleNode = new DoubleNode(data);
    DoubleNode current = first;
    //找值为key的节点
    while ((current != null) && (current.data != key)){
        current = current.next;
    }
    //若current==null,要么链表为空,要么没有值为key的数
    if (current == null){
       //根据题意来写... 
    }else{
        //这里还有可能key值是最后一个数,写法参考上面,这里不再考虑
        newDoubleNode.next=current.next;
        current.next.prev = newDoubleNode;
        newDoubleNode.pre=current;
        current.next=newDoubleNode;
    }
}

删除

删除头结点

public DoubleNode deleteFirst(){
    //如果只有一个节点。需要多考虑一个变量last
    if (first.next == null){
        last=null;
    }else{
        first.next.pre=null;
    }
    first=first.next;  
}

删除尾结点

public DoubleNode deleteLast(){
    //如果只有一个节点。需要多考虑一个变量first
    if (first.next == null){
        first=null;
    }else{
        last.pre.next=null;
    }
    last=last.pre;
}

按值删除

public DoubleNode deleteKey(int key){
    DoubleNode current = first;
    //寻找key值所在的节点
    while (current != null && current.data != key){
        current = current.next;
    }
    //若current==null,要么链表为空,要么没有值为key的数
    if (current == null){
       //根据题意来写...
        return null;
    }else{
        //这里还有可能key值是第一个或者最后一个数,写法参考上面,这里不再考虑 
        current.next.prev = current.pre;
        current.pre.next=current.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

endless_?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值