From 61e404723285eff74a5b314bc282113fa083e750 Mon Sep 17 00:00:00 2001 From: Yucheng Hu Date: Sat, 15 Dec 2018 15:16:24 -0500 Subject: [PATCH] =?UTF-8?q?lintcode=20=E4=B8=AD=E7=9A=84=E7=AC=AC=20102=20?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E6=A3=80=E6=9F=A5=E4=B8=80=E4=B8=AA=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E4=B8=AD=E6=98=AF=E5=90=A6=E5=B8=A6=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lintcode/LintCode0102HasCycleTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0102HasCycleTest.java diff --git a/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0102HasCycleTest.java b/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0102HasCycleTest.java new file mode 100644 index 0000000000..d1a7f16197 --- /dev/null +++ b/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0102HasCycleTest.java @@ -0,0 +1,64 @@ +package com.ossez.lang.tutorial.tests.lintcode; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.ossez.lang.tutorial.models.ListNode; + +/** + *

+ * 1480 + *

+ *

+ * + * @author YuCheng + * + */ +public class LintCode0102HasCycleTest { + + private final static Logger logger = LoggerFactory.getLogger(LintCode0102HasCycleTest.class); + + /** + * + */ + @Test + public void testMain() { + logger.debug("BEGIN"); + // INIT LINKED LIST + ListNode head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = new ListNode(3); + head.next.next.next = new ListNode(4); + + // CREATE A LOOP + head.next.next.next.next = head.next.next.next; + + boolean retResult = false; + + // LIKED LIST MAY NULL: + if (!(head == null || head.next == null)) { + ListNode s = head; + ListNode f = head.next; + + while (f.next != null && f.next.next != null) { + + s = s.next; + f = f.next.next; + + if (f == s) { + retResult = true; + break; + } + } + } + + System.out.println(retResult); + + } + +}