链表-单向链表、双向链表、链表反转、删除链表指定指定值

目录

单向链表

双向链表

链表反转

删除链表中指定的所有值


单向链表

package basic.linkedList;

public class SingleNode {

    public SingleNode next;

    public int value;

    public SingleNode(int value) {
        this.value = value;
    }


    public static SingleNode reverseSingleNode(SingleNode head){
        SingleNode next=null;
        SingleNode pre=null;
        while (head!=null){
            next=head.next;
            head.next=pre;
            pre=head;
            head=next;

        }
        return pre;
    }


    //删除链表中的值 要考虑删除的是头结点 或者是中间节点两种情况
    public static SingleNode removeValue(SingleNode head,int num){

        while (head!=null){//处理删除的值在头结点的情况
            if (head.value==num){
                head=head.next;
            }else {
                break;
            }
        }
        //经过上面处理,head来到了第一个不需要删除的节点的位置
        SingleNode pre=head;//用于维护一个节点的next值,辅助变量
        SingleNode cur=head;
        while (cur!=null){
            if (cur.value==num){
                pre.next = cur.next;
            }else {
                pre=cur;
            }
            cur=cur.next;
        }

        return head;
    }
}

双向链表

package basic.linkedList;

public class DoubleNode {

    public DoubleNode pre;
    public DoubleNode next;
    public int value;

    public DoubleNode(int value) {
        this.value = value;
    }

    public static DoubleNode reverseDoubleNode(DoubleNode head){
        DoubleNode next=null;
        DoubleNode pre=null;
        while (head!=null){
            next=head.next;
            head.next=pre;
            head.pre=next;
            pre=head;
            head=next;

        }
        return pre;
    }

    //删除链表中的值 要考虑删除的是头结点 或者是中间节点两种情况
    public static DoubleNode removeValue(DoubleNode head,int num){

        while (head!=null){//处理删除的值在头结点的情况
            if (head.value==num){
                head=head.next;
                head.pre=null;
            }else {
                break;
            }
        }
        //经过上面处理,head来到了第一个不需要删除的节点的位置
        DoubleNode pre=head;//用于维护一个节点的next值,辅助变量
        DoubleNode cur=head;
        while (cur!=null){
            if (cur.value==num){
                pre.next = cur.next;
                cur.next.pre=pre;
            }else {
                pre=cur;
            }
            cur=cur.next;
        }
        return head;
    }
}

链表反转

链表反转就是先保存next和pre,然后再调整,与数值交换差不多。

package basic.linkedList;

/**
 * 链表的指针反转
 * 给定头结点,返回反转后的头结点
 */
public class ReverseList {

    public static SingleNode reverseSingleNode(SingleNode head){
        SingleNode next=null;
        SingleNode pre=null;
        while (head!=null){
            next=head.next;
            head.next=pre;
            pre=head;
            head=next;

        }
        return pre;
    }

    public static DoubleNode reverseDoubleNode(DoubleNode head){
        DoubleNode next=null;
        DoubleNode pre=null;
        while (head!=null){
            next=head.next;
            head.next=pre;
            head.pre=next;
            pre=head;
            head=next;

        }
        return pre;
    }

}

删除链表中指定的所有值

删除链表中的值 要考虑删除的是头结点 或者是中间节点两种情况,剩下的就是链表指针的更改了

package basic.linkedList;

public class RemoveGivenValue {

    //删除链表中的值 要考虑删除的是头结点 或者是中间节点两种情况
    public static SingleNode removeValue(SingleNode head,int num){

        while (head!=null){//处理删除的值在头结点的情况
            if (head.value==num){
                head=head.next;
            }else {
                break;
            }
        }
        //经过上面处理,head来到了第一个不需要删除的节点的位置
        SingleNode pre=head;//用于维护一个节点的next值,辅助变量
        SingleNode cur=head;
        while (cur!=null){
            if (cur.value==num){
               pre.next = cur.next;
            }else {
                pre=cur;
            }
            cur=cur.next;
        }

        return head;
    }



    //删除链表中的值 要考虑删除的是头结点 或者是中间节点两种情况
    public static DoubleNode removeValue(DoubleNode head,int num){

        while (head!=null){//处理删除的值在头结点的情况
            if (head.value==num){
                head=head.next;
                head.pre=null;
            }else {
                break;
            }
        }
        //经过上面处理,head来到了第一个不需要删除的节点的位置
        DoubleNode pre=head;//用于维护一个节点的next值,辅助变量
        DoubleNode cur=head;
        while (cur!=null){
            if (cur.value==num){
                pre.next = cur.next;
                cur.next.pre=pre;
            }else {
                pre=cur;
            }
            cur=cur.next;
        }
        return head;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值