1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), ne...
分类:
其他好文 时间:
2015-03-20 08:03:53
阅读次数:
125
给出一个链表,假如这个链表有环,返回这个环的起点,否则返回null做法:用两个指针,p1 表示慢指针,p2表示快指针,快指针每次走2步,慢指针每次走一步,如果没环,必然是以p2->next->next = null或者p1 = null结束,如果有环p1与p2必然会相遇,当两指针相遇时使p1 指向h...
分类:
其他好文 时间:
2015-03-19 23:23:55
阅读次数:
97
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?这道...
分类:
其他好文 时间:
2015-03-18 23:06:39
阅读次数:
122
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?解题思路设链表长度为n,头结点与循环节点之间的长度为k。定义两个指针slow和fast,slow每次走...
分类:
其他好文 时间:
2015-03-18 14:07:38
阅读次数:
116
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull./** * Definition for singly-linked list. * class ListNod...
分类:
其他好文 时间:
2015-03-18 13:45:39
阅读次数:
107
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?runner 问题,注意while循环的条件。 1 /** 2 * Definit....
分类:
其他好文 时间:
2015-03-18 06:27:32
阅读次数:
114
Given a linked list, determine if it has a cycle in it.判断某个链表是否有环。方法一:用一个hashmap来存放访问过的节点,通过比较当前节点是否存在map中来判断是否有环:/** * Definition for singly-linked l...
分类:
其他好文 时间:
2015-03-17 21:45:57
阅读次数:
131
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?思路:...
分类:
其他好文 时间:
2015-03-16 12:38:20
阅读次数:
95
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:1.两个指针,一个走1步,一个走2步,如果相遇则有环注意: 在使用p->nex...
分类:
其他好文 时间:
2015-03-15 19:38:56
阅读次数:
126
/* 开始用hash做的,但这明显不是最优的*//** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : v...
分类:
其他好文 时间:
2015-03-15 15:11:29
阅读次数:
93