链表面试题(一)

1.反转一个单链表。

public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }//节点类
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public Node reverseList(){
        Node reverseHead=null;//反转后的新头节点
        Node prev=null;//cur的前驱
        Node cur=this.head;//cur代表的是当前需要反转的节点
        while (cur!=null){
            Node curNext=cur.next;
            if(curNext==null){
                reverseHead=cur;
            }
            cur.next=prev;
            prev=cur;
            cur=curNext;
        }
        return reverseHead;
    }
    }

2.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }//节点类
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public Node detectCycle(){
        Node fast=this.head;
        Node slow=this.head;
        while (fast!=null && fast.next.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow ){
                break;
            }
        }
        //没有环
        if(fast==null || fast.next.next==null){
            return null;
        }
        //将slow拉到头,然后和fast一人一步走
        slow=this.head;
        while (fast!=slow){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
    }

3.判断链表是否有环。

public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }//节点类
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public boolean hasCycle(){
        Node fast=this.head;
        Node slow=this.head;
        while (fast!=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }
    }

4.链表的回文结构。

public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }//节点类
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public boolean chkPalindrome(){
        Node fast=this.head;
        Node slow=this.head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //slow指向的位置就是中间位置
        Node p=slow.next;
        Node pNext=p.next;
        while (p!=null){
            p.next=slow;
            slow=p;
            p=p.next;
            if(p!=null){
                pNext=p.next;
            }
        }
        //后半部分已经进行反转
        while (this.head!=slow){
            if(this.head.data!=slow.data){
                return false;
            }
            if(this.head.next==slow){
                return true;
            }
            head=head.next;
            slow=slow.next;
        }
        return true;
    }
    }

5.在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头
指针。

public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }//节点类
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public Node deleteDuplication(){
        Node node=new Node(-1);
        Node cur=this.head;
        Node tmpHead=node;
        while (cur!=null){
            if(cur.next!=null&&cur.data==cur.next.data){
                while (cur.next!=null&&cur.data==cur.next.data){
                    cur=cur.next;
                }
                cur=cur.next;
                //cur指向的这个节点和前面的节点不同
                tmpHead.next=cur;
            }else{
                //确定不为重复的节点,串
             tmpHead.next=cur;
             tmpHead=cur;
             cur=cur.next;
            }
        }
        return node.next;
    }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值