题解 | #链表中环的入口结点#
链表中环的入口结点
http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
先用一个map存起来,然后一旦发现有相同的node与map中的相同就知道这个就是入口; import java.util.; / public class ListNode { int val; ListNode next = null;
ListNode(int val) {
this.val = val;
}
} */ public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
HashMap testMain = new HashMap();
while(pHead != null){
if(pHead.next == pHead){
return pHead;
}
if(pHead.next == null)
return null;
if(pHead.next == testMain.get(pHead.hashCode())){
return pHead;
}
testMain.put(pHead.hashCode(),pHead.next);
pHead = pHead.next;
}
return null;
}
}
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
print('Hello world!')