GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/Linked List Cycle II.txt at master · handongfeng/leetcode Expand file tree/ Copy pathLinked List Cycle II.txtMore file actions 50 lines (43 loc) · 1.14 KB / Copy pathLinked List Cycle II.txtFile metadata and controls50 lines (43 loc) · 1.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 /** * 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) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(!head) return 0; ListNode * p1 = head; ListNode * p2 = head; ListNode * p =0; int n =0; while(p2->next && p2->next->next) { p1=p1->next; p2 =p2->next->next; if(p1==p2) { p =p1; break; } } if(!p) return 0; else { p1 = head; if(p1==p) return p1; // if the current two node are same, we direct return it, this the border condition, be careful while(p1->next) { p1=p1->next; p=p->next; if(p1==p) return p1; } } } }; |
| You are visitor 01402809 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |