链表(Java)练习题

文章介绍了链表操作的各种方法,包括移除特定值的元素、反转链表、寻找中间节点、倒数第k个节点、按升序合并链表、检查链表的回文结构、相交链表以及判断链表是否存在环并找出环的起点。
摘要由CSDN通过智能技术生成

1.移除链表元素

class Solution {
    public ListNode removeElements(ListNode head, int val) {
            if(head == null){
                return null;
            }
            ListNode cur = head.next;
            ListNode prev = head;
            while(cur!=null){
                if(cur.val==val){
                    prev.next = cur.next;
                }
                else{
                    prev = cur;
                }
                cur=cur.next;
            }
            if(head.val == val){
                head = head.next;
            }
            return head;
    }
}

2. 将链表倒置

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode prev = null;
        ListNode mid = head;
        ListNode Next = head.next;
        while(mid!=null){
            mid.next = prev;
            prev = mid;
            mid = Next;
            if(Next!=null){
            Next = Next.next;
            }
        }
        return prev;
    }
}

3. 找到链表的中间节点

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

4. 找到链表的倒数第k个节点

class Solution {
    public ListNode kthToLast(ListNode head, int k) {
        ListNode x = head;
        int count = 0;
        while(x!=null && k<0){
            x = x.next;
            count++;
        }
        if(k>count){    //判断K是否合法
            return null;
        }
        for(int i = 0;i<count-k;i++){
            head = head.next;
        }
        return head;
    }
}

5. 按升序合并两个链表

class Solution {
    public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
            ListNode newH = new ListNode(-1);
            ListNode tmpH = newH;
            while(head1!=null&&head2!=null){
                if(head1.val<head2.val){
                    tmpH.next = head1;
                    head1 = head1.next;
                }
                else{
                    tmpH.next = head2;
                    head2 = head2.next;
                }
                tmpH = tmpH.next;
            }
            if(head1 == null){
                tmpH.next = head2;
            }
            if(head2 == null){
                tmpH.next = head1;
            }
            return newH.next;
    }
}

6.链表的回文结构

public class PalindromeList {
    public boolean chkPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
     while(fast!=null&&fast.next!=null){ //1.找到中间位置
            fast = fast.next.next;
            slow = slow.next;
       }  //2.反转slow之后的节点
        ListNode cur = slow.next;
    while(cur!=null){
        ListNode curNext = cur.next;
        cur.next = slow;
        slow = cur;
        cur = curNext;
        }
    while(head!=slow){  //3.从前到后,从后到前
        if(head.val!=slow.val){ //此时slow已经走到链表最后节点
            return false;
        }
        if(head.next==slow){
            return true;
        }
        head = head.next;
        slow = slow.next;
    }
    return true;
    }
}

7.相交链表

public class Solution {
    public ListNode getIntersectionNode(ListNode head1, ListNode head2) {
        int count1 = 0;
        int count2 = 0;
        int len = 0;
        ListNode a = head1;
        ListNode b = head2;
        while( a != null){
            a = a.next;
            count1++;
        }
        while(b != null){
            b = b.next;
            count2++;
        }
        len = count1-count2;
         a = head1;
         b = head2;
        if(len<0){
            a = head2;
            b = head1;
            len = -len;
        }
        for(int i=0;i<len;i++){
                a = a.next;
            }
        while(a!=b){
            a = a.next;
            b = b.next;
        }
        if(a == null){
            return null;
        }
        return a;
    }
}

7.判断链表是否带环

给你一个链表的头节点 head ,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。

如果链表中存在环 ,则返回 true 。 否则,返回 false 。

public class Solution {
    public boolean hasCycle(ListNode head) {
        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;
    }
}

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

public class Solution {
    public ListNode detectCycle(ListNode head) {
        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;
       }
       slow =  head;
       while(slow!=fast){
        slow = slow.next;
        fast = fast.next;
       }
       return slow;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值