Java-链表面试题(1)

import java.util.HashMap;
import java.util.Hashtable;

Class Node{
     int value;
     Node next;
}
Class CNode extends Node{
      Node random;
      CNode next;
      public CNode(int value){
      this.value=value;
      this.next=null;
      this.random=null;
      }
}
public class Solution {
    /**
     * 这里先处理除了头结点以外的其他节点,在最后处理头结点
     * @param head  链表头部元素
     * @param value  需要删除的值
     * @return  删除完需要删除的值之后返回链表头结点
     */
    public static Node removeAll1(Node head,int value){
        if(head==null){
            return head;
        }
        Node pre=head;
        Node cur=head.next;
        while(cur!=null){
            if(cur.value==value){
                pre.next=cur.next;
            }else{
                pre=cur;
            }
            cur=cur.next;
        }
        if(head.value==value){
            return head.next;
        }
        return head;
    }

    /**
     * 这里创建了一个假节点,处理直接是从head节点进行处理的
     * @param head 传入链表的头节点
     * @param value 需要删除的链表中的值
     * @return 删除完成的链表头节点
     */
    public static Node removeAll2(Node head,int value){
        Node fakeNode=new Node();
        fakeNode.next=head;
        Node pre=fakeNode;
        Node cur=head;
        while(cur!=null){
            if(cur.value==value){
                pre=cur.next;
            }else{
                pre=cur;
            }
            cur=cur.next;
        }
        return fakeNode.next;
    }

    /**
     *  在这里主要的特点是用传入的头结点作为返回值,如果head就是要删除的节点,则只需将head=head.next;
     * @param head  传入链表的头节点
     * @param value 需要删除的链表中的值
     * @return 删除完成的链表头节点
     */
    public static Node removeAll3(Node head,int value){
        Node pre=null;
        Node cur=head;

        while(cur!=null){
            if(cur.value==value){
                if(pre==null){
                    head=head.next;
                }else{
                    pre.next=cur.next;
                }
            }else{
                pre=cur;
            }
            cur=cur.next;
        }
        return head;
    }
    /**
     *新建一个链表将于value值不相等的节点搬至新链表,new了一个假头结点,只需要动lastNode 节点就行了
     * @param head  传入链表的头节点
     * @param value 需要删除的链表中的值
     * @return 删除完成的链表头节点
     */
    public static Node removeAll4(Node head,int value){
        if(head==null){
            return head;
        }
        Node cur=head;
        Node newNode=new Node();
        Node lastNode=newNode;
        while(cur!=null){
            if(cur.value!=value){
                 lastNode.next=cur;
                 lastNode=cur;

            }
                cur=cur.next;//不论搬与不搬它都要往后走

        }
        lastNode.next=null;//记得最后将节点next域置空
        return newNode.next;
    }

    /**
     *新建一个头结点和为节点,维护头结点和为结点,cur指向当前操作的节点
     * @param head  传入链表的头节点
     * @param value 需要删除的链表中的值
     * @return 删除完成的链表头节点
     *
     */
    public static Node removeAll5(Node head,int value){
        Node cur=head;
        Node frontNode=null;
        Node lastNode=null;
        while(cur!=null){
            if(cur.value!=value){
                if(frontNode==null){
                    frontNode=cur;
                }else{
                    lastNode.next=cur;
                }
                lastNode=cur;
            }
              cur=cur.next;
        }
        if(lastNode!=null){
            lastNode.next=null;
        }
        return frontNode;
    }

    /**
     *  链表的反转,将原来的链表中节点,采用头插法插入到新链表
     * @param head 传入一条链表
     * @return  返回逆置之后链表
     */
    public static Node reverse1(Node head){
        Node cur=head;
        Node next=null;
        Node newHead=null;
        while(cur!=null){
            next=cur.next;
            cur.next=newHead;
            newHead=cur;
            cur=next;
        }
        return newHead;
    }

    /**
     * 这里是采用三个指针的形式进行链表的逆置。向后遍历每个节点,但是要特别注意的是,空指针解引用会抛出异常
     * @param head
     * @return
     */
    public static Node reverse2(Node head){
        if(head==null){
            return null;
        }
        Node p1=null;
        Node p2=head;
        Node p3=head.next;

        while(p2!=null){
            p2.next=p1;
            p1=p2;
            p2=p3;

            if(p3!=null){
                p3=p3.next;
            }
        }
        return p1;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值