iT邦幫忙

0

【LeetCode with C: A Series of Problem-Solving Techniques】-- Linked List Cycle

  • 分享至 

  • xImage
  •  

Description

Given head, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. Otherwise, return false.

Example 1:
截圖 2024-09-29 中午12.46.46

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
截圖 2024-09-29 中午12.48.23

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
截圖 2024-09-29 中午12.48.41

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Constraints:

The number of the nodes in the list is in the range [0, 104].
-105 <= Node.val <= 105
pos is -1 or a valid index in the linked-list.

Follow up: Can you solve it using O(1) (i.e. constant) memory?

Answer & Explaining

bool hasCycle(struct ListNode *head) {
    if (head == NULL || head->next == NULL) {
        return false; //head不存在或只有一點
    }
    
    //創建兩指標指向head
    struct ListNode *slow = head; 
    struct ListNode *fast = head;
    
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;// slow一次走一步
        fast = fast->next->next;//fast一次走兩步
        
        if (slow == fast) { //兩者相遇
            return true; //有環
        }
    }
    
    return false;//無環
}

Testing

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// 單向鏈結串列的定義。
struct ListNode { //創建structure
    int val;
    struct ListNode *next;
};

bool hasCycle(struct ListNode *head) {
    if (head == NULL || head->next == NULL) {
        return false;
    }
    
    struct ListNode *slow = head;
    struct ListNode *fast = head;
    
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
        
        if (slow == fast) {
            return true;
        }
    }
    
    return false;
}

// 幫助函數來創建一個新的節點
struct ListNode* createNode(int val) {
    struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode));
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

// 範例使用
int main() {
    // 創建節點
    struct ListNode* node1 = createNode(3);
    struct ListNode* node2 = createNode(2);
    struct ListNode* node3 = createNode(0);
    struct ListNode* node4 = createNode(-4);

    // 建立鏈結串列並形成循環
    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = node2;  // 形成循環,尾節點指向第二個節點

    // 檢測循環
    if (hasCycle(node1)) {
        printf("鏈結串列中存在循環。\n");
    } else {
        printf("鏈結串列中不存在循環。\n");
    }

    return 0;
}


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言