单链表习题(下)

  • 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
public void display(Node newHead){
        Node cur = newHead;
        while(cur != null){
            System.out.print(cur.getData()+" ");
            cur = cur.getNext();
        }
        System.out.println();
    }
    public Node deleteDuplication(){
        //定义一个虚拟节点(傀儡节点)
        Node node = new Node(-1);
        //定义tmp来保存虚拟节点的next域
        Node tmp = node;
        Node cur = this.head;
        //遍历单链表
        while (cur != null){
            if ((cur.getNext() != null) && (cur.getData() == cur.getNext().getData())){
                while ((cur.getNext() != null) && (cur.getData() == cur.getNext().getData())){
                    cur = cur.getNext();
                }
                cur = cur.getNext();
            }else{
               tmp.setNext(cur);
               tmp = cur;
               cur = cur.getNext();
            }
        }
        //此处tmp表示单链表的尾巴,如果未置为null,那么打印时会出现死循环
        tmp.setNext(null);
        //node为虚拟节点,此处的返回值为了防止原单链表的head就是重复节点
        return node.getNext();
    }
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(1);
        myLinkedList.addLast(2);
        myLinkedList.addLast(3);
        myLinkedList.addLast(3);
        myLinkedList.addLast(4);
        myLinkedList.addLast(4);
        myLinkedList.addLast(5);
        Node newHead = myLinkedList.deleteDuplication();
        myLinkedList.display(newHead);
    }
}
  • 链表的回文结构对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。所谓回文结构就是指形如:1,2,2,1又或是1,2,3,2,1这样的“对称”的链表!
public boolean chkPalindrome(){
        //定义fast和slow两个变量,首先找到中间位置
        Node slow = this.head;
        Node fast = this.head;
        while(fast != null && fast.getNext() != null){
            fast = fast.getNext().getNext();
            slow = slow.getNext();
        }
        //接下来对中间位置靠右边的data进行逆置
        Node cur = slow.getNext();
        while(cur != null){
            Node curNext = cur.getNext();
            cur .setNext(slow);
            slow = cur;
            cur = curNext;
        }
        //判断是否为回文结构
        while(this.head != slow){
            if (this.head.getData() != slow.getData()){
                return false;
            }
            //偶数情况
            if (this.head.getNext() == slow){
                return true;
            }
            this.head = this.head.getNext();
            slow = slow.getNext();
        }
        return true;
    }
  • 输入两个链表,找出它们的第一个公共结点。
public class TestDemo {
    //创造交点
    public static void createMeet(Node headA,Node headB){
        headA.getNext().setNext(headB.getNext().getNext());
    }
    public static Node getIntersectionNode(Node headA,Node headB) {
        if (headA == null || headB == null) {
            return null;
        }
        Node pL = headA;//长的单链表
        Node pS = headB;//短的单链表
        int lA = 0;//表示A链表的长度
        int lB = 0;
        //求链表的长度
        while(pL != null){
            lA++;
            pL = pL.getNext();
        }
        while(pS != null){
            lB++;
            pS = pS.getNext();
        }
        //此时pL,pS均为null,然后要将将它们指回原来的headA,headB
        pL = headA;
        pS = headB;
        //找到较长的单链表,并让此单链表先走差值步
        int len = lA - lB;
        if (len < 0){
            pL = headB;
            pS = headA;
            len = lA - lB;
        }
        //可以保证pL指向了长的单链表
        while(len > 0){
            pL = pL.getNext();
            len--;
        }
        while (pL != pS){
            pL = pL.getNext();
            pS = pS.getNext();
        }
        if (pL != null && pS != null && pS == pL){
            return pL;
        }
        return null;
    }
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(10);
        myLinkedList.addLast(30);
        myLinkedList.addLast(50);
        myLinkedList.addLast(70);
        myLinkedList.display();
        MyLinkedList myLinkedList1 = new MyLinkedList();
        myLinkedList1.addLast(20);
        myLinkedList1.addLast(40);
        myLinkedList1.addLast(60);
        myLinkedList1.addLast(80);
        myLinkedList1.addLast(100);
        myLinkedList1.display();
        System.out.println("===========");
        createMeet(myLinkedList.head,myLinkedList1.head);
        Node ret = getIntersectionNode(myLinkedList.head,myLinkedList1.head);
        System.out.println(ret.getData());
    }
}
  • 给定一个链表,判断链表中是否有环
//创造环
    public void createLoop(){
        Node cur = this.head;
        while(cur.getNext() != null){
            cur = cur.getNext();
        }
        cur.setNext(this.head.getNext().getNext());
    }
    public boolean hasCycle(){
        Node fast = this.head;
        Node slow = this.head;
        while ((fast != null) && (fast.getNext() != null)){
            fast = fast.getNext().getNext();
            slow = slow.getNext();
            if (fast == slow){
                break;
            }
        }
        if (fast == null || fast.getNext() == null){
            return false;
        }
        return true;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值