Ch2-5: find the beginning of loop in a circular linked list---two solutions
2014-01-11 09:48
525 查看
Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C
这题有2中做法,书上的那个还没仔细看(2个指针,一个+2,一个+1,如果重复了,就说明有环。但是怎么找到环的头节点呢?关键是:
在重复的时候,把slow重新指向head,同时把fast的速度改为一,那么他们再次相遇的时候就是环路的头了)公式请参考hawstein。
I thinks the 2nd idea is simple as similar to early solution: use a hashmap to have key(node addr), if the addr is duplicate, it means there is a loop.
输出结果:
Executing the program....
$demo
3
DEFINITION
Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C
这题有2中做法,书上的那个还没仔细看(2个指针,一个+2,一个+1,如果重复了,就说明有环。但是怎么找到环的头节点呢?关键是:
在重复的时候,把slow重新指向head,同时把fast的速度改为一,那么他们再次相遇的时候就是环路的头了)公式请参考hawstein。
I thinks the 2nd idea is simple as similar to early solution: use a hashmap to have key(node addr), if the addr is duplicate, it means there is a loop.
#include #include using namespace std; typedef struct node{ int data; node *next; }node; // initialize a linkedlist with a loop, // the n is the total nodes, m is the starting // node of the loop, still a singly linked list node* init(int *a, int n, int m){ node *head, *p, *q; for(int i=0; idata = a[i]; if(i==m) q = nd; if(i==0){ head = p = nd; continue; } p->next = nd; p = nd; } p->next = q; return head; } // seems like a math contest idea node* loopstart0(node *head){ // see the explaination node *fast = head, *slow=head; while(fast || fast->next){ fast = fast->next->next; slow = slow->next; if(fast==slow) break; } if(!fast || !fast->next) return NULL; slow = head; while(fast!=slow){ fast = fast->next; slow = slow->next; } return fast; } // method 2: simple and easy to understand map hashmap; node* loopstart1(node *head){ while(head){ if(hashmap[head]) return head; else{ hashmap[head] = true; head = head->next; } } return head; } int main(){ int n =6, m =2 ;// mdata<
输出结果:
Executing the program....
$demo
3
相关文章推荐
- 35.两链表的第一个公共结点[Find the first common node of two linked list]
- 找出有环链表的起点 find the start point in loop linked list
- Find the nth to last element of a singly linked list
- Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- 164 Using the LIST command in Recovery Manager (RMAN), which two pieces of information from the RMAN
- Find the two non-repeating elements in an array of repeating elements
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- LintCode 372. Delete Node in the Middle of Singly Linked List
- python: find the index of a given value in a list
- LintCode: Delete Node in the Middle of Singly Linked List
- Find the common ancestor of two nodes in a binary tree
- Q2.2 Find the kth to last element of a singly linked list
- 【11】Delete a node in the middle of a single linked list
- LintCode 372: Delete Node in the Middle of Singly Linked List
- Two Solutions to fix issue: ORDER BY items must appear in the select list if the statement contains a UNION, ...
- [CrackCode] 2.2 Find the nth to last element of a singly linked list
- To find the kth to Last Element of a Singly Linked List
- Find the starting point of a linked list
- [CrackCode] 2.3 Delete a node in the middle of a single linked list