Java链表面试之11道经典例题

1.反转链表

 //2.反转一个链表
        public ListNode reverseList(){
            if(this.head == null){
                System.out.println("链表为空!");
                return null;
            }
            ListNode cur = this.head;
            ListNode prev = null;
            while (cur != null){
                ListNode curNext = cur.next;
                cur.next = prev;
                prev = cur;
                cur.next = prev;
            }
            return prev;

        }

 2.给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回  第二个中间结点

public ListNode middleNode() {
            if(head == null){
                return null;
            }
            ListNode fast = this.head;
            ListNode slow = this.head;
            while (fast != null ||fast.next != null){  //两个判断条件不能写反,否则错误
                fast=fast.next.next;
                slow = slow.next;
            }
            return slow;
        }

如果是偶数个节点,返回中间第一个节点:

public ListNode middleNode() {
            if(head == null){
                return null;
            }
            ListNode fast = this.head;
            ListNode slow = this.head;
            while (fast != null ||fast.next != null){
                fast = fast.next.next;
                if(fast == null){
                    return slow;
                }
                slow = slow.next;
            }
            return slow;
        }

3. 输入一个链表,输出该链表中倒数第k个结点

//输入一个链表,输出该链表中倒数第k个结点
         public ListNode findKthToTail(int k){
            if(k < 0 || this.head == null){
                return null;
            }
            ListNode fast = this.head;
            ListNode slow = this.head;
            while (k-1 != 0){
                fast = fast.next;
                if(fast == null){
                    return null;
                }
                k--;
            }
            while(fast.next != null){
                fast = fast.next;
                slow = slow.next;
            }
            return slow;

         }


 

4. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点 组成的

public class TestDemo {
    //将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
    public static ListNode mergeTwoLists(ListNode HeadA,ListNode HeadB){

        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (HeadA != null && HeadB != null){
            if(HeadA.val < HeadB.val){
                tmp.next=HeadA;
                HeadA = HeadA.next;
                tmp=tmp.next;
            }else{
                tmp.next = HeadB;
                HeadB = HeadB.next;
                tmp = tmp.next;
            }
        }
        if(HeadA == null){
            tmp.next = HeadB;
        }else{
            tmp.next = HeadA;
        }
        return newHead.next;
    }

 5.以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前

public ListNode partition(int x){
            ListNode bs = null;
            ListNode be = null;
            ListNode as = null;
            ListNode ae = null;
            ListNode cur = this.head;
         while (cur != null){
             if(cur.val < x){
                 //第一次插入数据
                 if(bs == null){
                     bs = cur;
                     be = cur;
                 }else{
                     //不是第一次插入数据,则是尾插
                     be.next = cur;
                     be = be.next;
                 }
             }else{
                 if(as == null){
                     as = cur;
                     ae = cur;
                 }else{
                     ae.next = cur;
                     ae = ae.next;
                 }
             }
             cur = cur.next;
         }
         //防止第一段为空
         if(bs == null){
             return as;
         }
         be.next = as;
         //防止第二段最后的节点没有置为空
        if(as != null){
          as.next = null;
        }
        return bs;
        }

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

public ListNode deleteDuplication() {
            ListNode cur = head;
            ListNode newHead = new ListNode(-1);
            ListNode tmp = newHead;
            while(cur != null){
                if(cur.next != null && cur.val == cur.next.val){
                    while (cur.next != null && cur.val == cur.next.val){
                        cur = cur.next;
                    }
                    cur = cur.next;
                }else{
                    tmp = cur;
                    cur = cur.next;
                    tmp = tmp.next;
                }
            }
            tmp.next = null;
            return newHead.next;

        }

7. 判断链表是否是回文结构

public boolean chkPalindrome(ListNode A){
            if(head == null){
                return true;
            }
            //找中间节点
            ListNode fast = this.head;
            ListNode slow = this.head;
            while(fast != null && fast.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }
            //反转
            ListNode cur = slow.next;
            while(cur != null ){
                ListNode curNext = cur.next;
                cur.next = slow;
                slow = cur;
                cur = curNext;
            }
            while(head != slow){
                if(head.val != slow.val){//奇数情况下
                    return false;
                }
                if(head.next == slow ){//偶数情况下
                    return true;
                }
                head = head.next;
                fast = fast.next;
            }
            return true;
        }

8.输入两个链表,找出它们的第一个公共结点

//输入两个链表,找出它们的第一个公共结点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        while (pl != null) {
            lenA++;
            pl = pl.next;
        }
        pl = headA;
        while (ps != null) {
            lenB++;
            ps = ps.next;
        }
        ps = headB;
        int len = lenA - lenB;
        if (len < 0) {
            pl = headB;
            ps = headA;
            len = pl - ps;
        }
        //pl永远指向最长的链表,ps永远指向最短的链表
        //长的先走差值len步        while(len != 0){
        pl = pl.next;
        len--;

        //两个同时走,直到相遇
        while (pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        return pl;
    }

 

9.给定一个链表,判断链表中是否有环
 


            public boolean hasCycle() {
                if(head == null) return false;
                ListNode fast = head;
                ListNode slow = head;
                while(fast != null && fast.next != null) {
                    fast = fast.next.next;
                    slow = slow.next;
                    if(fast == slow) {
                        return true;
                    }
                }
                return false;
            }

 

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

public ListNode detectCycle(ListNode head){
                if(head == null) return false;
                ListNode fast = head;
                ListNode slow = head;
                while(fast != null && fast.next != null) {
                    fast = fast.next.next;
                    slow = slow.next;
                    if(fast == slow){
                        break;
                    }
                }
                if(fast == null || fast.next == null){
                    return null;
                }
                fast = head;
                while(fast != null){
                    fast = fast.next;
                    slow = slow.next;
                }
                return fast;
          }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值