四种语言刷算法之环形链表 II

力扣142. 环形链表 II

1、C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    if (head==NULL||head->next==NULL)return NULL;
    struct ListNode* p = head->next;
    struct ListNode *q = head->next->next;
    while(p!=NULL&&q!=NULL){
        if(p==q){
            p = head;
            while(p!=q){
                p = p->next;
                q = q->next;
            }
            return p;
        }
        else{
            p = p->next;
            if(q->next!=NULL){
                q = q->next->next;
            }
            else{
                break;
            }
        }
    }
    return NULL;
}

2、C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
       if(head==NULL||head->next==NULL)return NULL;
       ListNode *p = head->next;
       ListNode *q = head->next->next;
       while(p!=NULL&&q!=NULL){
           if(p==q){
               p = head;
               while(p!=q){
                   p = p->next;
                   q = q->next;
               }
               return p;
           }
           else{
               p = p->next;
               if(q->next!=NULL){
                   q = q->next->next;
               }
               else{
                   return NULL;
               }
           }
       }
       return NULL;
    }
};

3、JAVA

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null||head.next==null){
            return null;
        }
        ListNode p = head.next;
        ListNode q = head.next.next;
        while(p!=null&&q!=null){
            if(p==q){
                p = head;
                while(p!=q){
                    p = p.next;
                    q = q.next;
                }
                return p;
            }
            else{
                p = p.next;
                if(q.next==null){
                    return null;
                }
                else{
                    q = q.next.next;
                }
            }
        }
        return null;
    }
}

4、Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return None
        p = head.next;
        q = head.next.next
        while(p is not None and q is not None):
            if p == q:
                p = head
                while(q!=p):
                    p = p.next
                    q = q.next
                return p
            else:
                p = p.next
                if q.next is None:
                    return None
                else:
                    q = q.next.next
        return None
posted @ 2023-05-08 08:59  菜鸟冲冲冲  阅读(21)  评论(0)    收藏  举报