leetcode之Linked List Cycle
2014-04-03 22:34
375 查看
题目意思:看一个链表中是否有环。注:不能用额外的空间。
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
解决思路:
1,首先,如果能用额外的空间,可以把访问过的节点存储到vector中,然后判断是否有重复,如果有重复,则有环。
2,定义两个指针,一个快的,每次走两步,一个慢的,每次走一步。如果他们相遇,则表示链表中有环,且相遇点一定在环里。
代码如下:
leetcode之Linked List CycleII
题目大意:
Given a linked list, return the node where the cycle begins. If there is no cycle, return
Follow up:
Can you solve it without using extra space?
意思就是:
给定一个链表,找出环的开始点.如果没有环,则返回NULL
做题思路:
首先利用上述第一题的方法找出是否有环.如果有环,则利用性质: 指定一个新指针,指向快指针和慢指针相遇的点, 然后再指定一个指针,指向head指针出发,两个新指针同时出发, 他们相遇的点即为环的开始点.
代码如下:
另:
第一题和第二题重新简写过代码,分别如下:
第一题:
第二题:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
解决思路:
1,首先,如果能用额外的空间,可以把访问过的节点存储到vector中,然后判断是否有重复,如果有重复,则有环。
2,定义两个指针,一个快的,每次走两步,一个慢的,每次走一步。如果他们相遇,则表示链表中有环,且相遇点一定在环里。
代码如下:
class Solution { public: bool hasCycle(ListNode *head) { ListNode* slow_travel = head; ListNode* fast_travel = head; while(slow_travel){ slow_travel = slow_travel->next; if(slow_travel == NULL){ return false; } fast_travel = fast_travel->next; if(fast_travel == NULL){ return false; } fast_travel = fast_travel->next; if(fast_travel == NULL){ return false; } if((fast_travel->val == slow_travel->val) && (fast_travel->next->val == slow_travel->next->val)){ return true; } } return false; } };
leetcode之Linked List CycleII
题目大意:
Given a linked list, return the node where the cycle begins. If there is no cycle, return
null.
Follow up:
Can you solve it without using extra space?
意思就是:
给定一个链表,找出环的开始点.如果没有环,则返回NULL
做题思路:
首先利用上述第一题的方法找出是否有环.如果有环,则利用性质: 指定一个新指针,指向快指针和慢指针相遇的点, 然后再指定一个指针,指向head指针出发,两个新指针同时出发, 他们相遇的点即为环的开始点.
代码如下:
/** * 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){ pair<ListNode*, bool> ifCycle = hasCycle(head); ListNode* slow_travel = NULL; ListNode* fast_travel = NULL; int step = 0; if(!ifCycle.second){ return NULL; } else{ slow_travel = ifCycle.first; fast_travel = ifCycle.first; while(1){ slow_travel = slow_travel->next; fast_travel = fast_travel->next; fast_travel = fast_travel->next; step++; if((slow_travel->val == fast_travel->val) && (slow_travel->next->val == fast_travel->next->val)){ break; } } } fast_travel = head; slow_travel = head; while(step){ fast_travel = fast_travel->next; step--; } while(true){ if((fast_travel->val == slow_travel->val) && (fast_travel->next->val == slow_travel->next->val)){ break; } fast_travel = fast_travel->next; slow_travel = slow_travel->next; } return slow_travel; } pair<ListNode*, int> hasCycle(ListNode* head){ ListNode* slow_travel = head; ListNode* fast_travel = head; pair<ListNode*, int> ifCycle; while(slow_travel){ slow_travel = slow_travel->next; if(slow_travel == NULL){ ifCycle.first = NULL; ifCycle.second = false; return ifCycle; } fast_travel = fast_travel->next; if(fast_travel == NULL){ ifCycle.first = NULL; ifCycle.second = false; return ifCycle; } fast_travel = fast_travel->next; if(fast_travel == NULL){ ifCycle.first = NULL; ifCycle.second = false; return ifCycle; } if((fast_travel->val == slow_travel->val) && (fast_travel->next->val == slow_travel->next->val)){ ifCycle.first = fast_travel; ifCycle.second = true; return ifCycle; } } ifCycle.first = NULL; ifCycle.second = false; return ifCycle; } };
另:
第一题和第二题重新简写过代码,分别如下:
第一题:
struct ListNode{ int val; ListNode* next; ListNode(int x) : val(x), next(NULL){} }; class Solution{ public: bool hasCycle(ListNode* head){ if(head == NULL){ return false; } ListNode* fast = head; ListNode* slow = head; while(slow){ fast = fast->next; if(fast == NULL){ return false; } fast = fast->next; if(fast == NULL){ return false; } slow = slow->next; if(slow == NULL){ return false; } if(fast == slow){ return true; } } } };
第二题:
struct ListNode{ int val; ListNode* next; ListNode(int x) : val(x), next(NULL){} }; class Solution{ public: ListNode* deterCycle(ListNode* head){ pair<bool, ListNode*> ifCycle = LinkedListCycle(head); if(ifCycle.first){ ListNode* current = head; ListNode* collision = ifCycle.second; while(true){ current = current->next; collision = collision->next; if(current == collision){ return current; } } } else{ return NULL; } } pair<bool, ListNode*> LinkedListCycle(ListNode* head){ pair<bool, ListNode*> result = make_pair(false, (ListNode*)NULL); if(head == NULL){ return result; } ListNode* slow = head; ListNode* fast = head; while(slow){ fast = fast->next; if(fast == NULL){ return result; } fast = fast->next; if(fast == NULL){ return result; } slow = slow->next; if(slow == NULL){ return result; } if(fast == slow){ result.first = true; result.second = slow; return result; } } } };
相关文章推荐
- LeetCode(1) -- Linked List Cycle
- LeetCode Linked List Cycle II
- [leetcode] Linked List Cycle II
- LeetCode142:Linked List Cycle II
- 【leetcode】Linked List Cycle
- [Leetcode] Linked List Cycle (Java)
- leetcode day5 -- Reorder List && Linked List Cycle II
- [Leetcode]Linked List Cycle II
- 【leetcode82】Linked List Cycle
- leetcode笔记--Linked List Cycle
- [leetcode142]Linked List Cycle II
- (leetcode)Linked List Cycle
- leetcode:linked_list_cycle_II
- Leetcode Linked List Cycle II
- leetcode_c++:链表:Linked List Cycle(141)
- LeetCode 11 Linked List Cycle
- Leetcode Linked List Cycle 循环链表
- Leetcode 142 Linked List Cycle II
- 【LeetCode】【Python】Linked List Cycle
- [LeetCode]Linked List Cycle II