数据结构 链表OJ题

【数据结构】链表OJ题

文章目录

  • 【数据结构】链表OJ题
      • 1. 删除链表中等于给定值val的所有节点
      • 2. 翻转链表
      • 3. 获取链表的中间节点
      • 4. 获取链表中倒数第k个节点
      • 5. 合并两个有序链表
      • 6. 链表分割
      • 7. 链表的回文结构
      • 8. 相交链表
      • 9. 环形链表
      • 10.环形链表II

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

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

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

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

数据结构 链表OJ题_第1张图片

代码示例:

 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;
            }
        }
    }

数据结构 链表OJ题_第2张图片

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;
    }

数据结构 链表OJ题_第3张图片

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;
    }

数据结构 链表OJ题_第4张图片

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;
    }

数据结构 链表OJ题_第5张图片

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;
    }

数据结构 链表OJ题_第6张图片

6. 链表分割

【OJ链接】
数据结构 链表OJ题_第7张图片

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;
    }

数据结构 链表OJ题_第8张图片

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;
    }

数据结构 链表OJ题_第9张图片

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;

    }

数据结构 链表OJ题_第10张图片

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;
    }

数据结构 链表OJ题_第11张图片

你可能感兴趣的:(数据结构,数据结构,链表)