数据结构 链表OJ题

【数据结构】链表OJ题


结合之前所学习的链表知识,我们来刷一些相应的OJ题巩固知识:

1. 删除链表中等于给定值val的所有节点

OJ链接
先设置prev指针和cur指针用来记录所处链表节点位置

prev为当前要删除节点的前驱,cur为当前要删除的节点

在这里插入图片描述

代码示例:

 public void removeAllKey(int key) {
        if (this.head == null) {
            System.out.println("链表为空");
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while(cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }
            else {
                prev = cur;
                cur = cur.next;
            }
        }
    }

在这里插入图片描述

2. 翻转链表

OJ链接

代码示例:

public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }

        ListNode cur = head;
        head = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

在这里插入图片描述

3. 获取链表的中间节点

OJ链接

代码示例:

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个节点

OJ链接

代码示例:

public ListNode FindKthToTail(ListNode head,int k) {
        if (head == null) {
            return null;
        }
        
        ListNode fast = head;
        ListNode slow = head;

        ListNode cur = head;
        int sum = 0;
        while(cur != null) {
            cur = cur.next;
            sum++;
        }
        if (k <= 0 || k > sum) {
            return null;
        }

        int count = 0;
        while (count < k - 1) {
            fast = fast.next;
            count++;
        }

        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

在这里插入图片描述

5. 合并两个有序链表

OJ链接

代码示例:

 public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
            ListNode newNode = new ListNode(-1);
            ListNode cur = newNode;

            while(head1 != null && head2 != null) {
                if (head1.val < head2.val) {
                    cur.next = head1;
                    cur = head1;
                    head1 = head1.next;
                }else {
                    cur.next = head2;
                    cur = head2;
                    head2 = head2.next;
                }
            }
            if (head1 == null) {
                cur.next = head2;
            }
            if (head2 == null) {
                cur.next = head1;
            }

            newNode = newNode.next;
            return newNode;
    }

在这里插入图片描述

6. 链表分割

OJ链接
在这里插入图片描述

7. 链表的回文结构

OJ链接

代码示例:

public boolean chkPalindrome() {
        // write code here
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow;
        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;
            slow = slow.next;
        }
        return true;
    }

在这里插入图片描述

8. 相交链表

OJ链接

代码示例

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode cur1 = headA;
        ListNode cur2 = headB;
        int len1 = 0, len2 = 0;

        while(cur1 != null) {
            len1++;
            cur1 = cur1.next;
        }
        while(cur2 != null) {
            len2++;
            cur2 = cur2.next;
        }

        if (len1 > len2) {
            int x = len1 - len2;
            for (int i = 0;i < x;i++) {
                headA = headA.next;
            }
        }
        else {
            int x = len2 - len1;
            for (int i = 0;i < x;i++) {
                headB = headB.next;
            }
        }
        while (headA != headB) {
            headA = headA.next;
            headB = headB.next;
            
        }
        if (headA != null) {
            return headA;
        }
        return null;
    }

在这里插入图片描述

9. 环形链表

OJ链接

代码示例:

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;

    }

在这里插入图片描述

10.环形链表II

OJ链接
代码示例:

 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) {
                slow = head;
                while(fast != slow) {
                    fast = fast.next;
                    slow = slow.next;
                }
                return slow;
            }
        }
        return null;
    }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值