给定一个链表,若其中包含环,则输出环的入口节点。
若其中不包含环,则输出null
。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *entryNodeOfLoop(ListNode *head) { auto a = head, b = head; while (b) { a = a->next; b = b->next; if (b) b = b->next; else return NULL; if (a == b) { a = head; while (a != b) { a = a->next; b = b->next; } return a; } } return NULL; } };
标签:结点,ListNode,中环,head,next,链表,return,NULL From: https://www.cnblogs.com/leetothemoon/p/16983897.html