From 4eca5394ef17a624a41c873288cf7acae00b48e0 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 15 Dec 2018 20:26:30 +0800 Subject: [PATCH] complete checkCircle and reserve --- .../ch07_linkedlist/LinkedListAlgo.scala | 58 ++++++++++++++ .../ch07_linkedlist/LinkedListAlgoTest.scala | 76 +++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 scala/src/main/scala/ch07_linkedlist/LinkedListAlgo.scala create mode 100644 scala/src/test/scala/ch07_linkedlist/LinkedListAlgoTest.scala diff --git a/scala/src/main/scala/ch07_linkedlist/LinkedListAlgo.scala b/scala/src/main/scala/ch07_linkedlist/LinkedListAlgo.scala new file mode 100644 index 0000000..91b8d66 --- /dev/null +++ b/scala/src/main/scala/ch07_linkedlist/LinkedListAlgo.scala @@ -0,0 +1,58 @@ +package ch07_linkedlist + +import ch06_linkedlist.Node + +object LinkedListAlgo { + + //reverse a linked list + def reverse(head: Node): Node = { + if (head.next.isEmpty) { + //single node of linked list + return head + } + var prev: Option[Node] = None + var current: Option[Node] = Some(head) + var next: Option[Node] = None + + while (current.get.next.isDefined) { + next = current.get.next + current.get.next = prev + + prev = current + current = next + } + //for the tail of the node, make it point to previous node to complete the reverse + current.get.next = prev + + current.get + } + + def checkCircle(head: Node): Boolean = { + var fast = head + var slow = head + + while (fast.next.isDefined && fast.next.get.next.isDefined) { + fast = fast.next.get.next.get + slow = slow.next.get + + if (fast.equals(slow)) { + return true + } + } + false + } + + //form all the chain value as string + def mkStringForChain(node: Node): String = { + val result = new StringBuilder + + var p = node + + while (p.next.isDefined) { + result.append(p.data) + p = p.next.get + } + result.append(p.data) + result.mkString + } +} diff --git a/scala/src/test/scala/ch07_linkedlist/LinkedListAlgoTest.scala b/scala/src/test/scala/ch07_linkedlist/LinkedListAlgoTest.scala new file mode 100644 index 0000000..d8bea3c --- /dev/null +++ b/scala/src/test/scala/ch07_linkedlist/LinkedListAlgoTest.scala @@ -0,0 +1,76 @@ +package ch07_linkedlist + +import ch06_linkedlist.{Node, SinglyLinkedList} +import org.scalatest.{FlatSpec, Matchers} + +class LinkedListAlgoTest extends FlatSpec with Matchers { + + behavior of "LinkedListAlgoTest" + + it should "reverse a node for long linked list" in { + val list = new SinglyLinkedList() + + for (i <- 0 to 9) { + list.insertTail(i) + } + LinkedListAlgo.mkStringForChain(list.headOpt.get) should equal((0 to 9).toArray.mkString("")) + + val reverseNode = LinkedListAlgo.reverse(list.headOpt.get) + + LinkedListAlgo.mkStringForChain(reverseNode) should equal((9 to 0 by -1).toArray.mkString("")) + } + + it should "reverse a node for linked list with 1 node" in { + val list = new SinglyLinkedList() + + list.insertTail(100) + + LinkedListAlgo.mkStringForChain(list.headOpt.get) should equal("100") + + val reverseNode = LinkedListAlgo.reverse(list.headOpt.get) + + LinkedListAlgo.mkStringForChain(reverseNode) should equal("100") + } + + it should "check circle for circled linked list" in { + + val node1 = new Node(1, None) + val node2 = new Node(2, None) + val node3 = new Node(3, None) + val node4 = new Node(4, None) + val node5 = new Node(5, None) + val node6 = new Node(6, None) + + //1->2->3->4->5->6->3 it's a circle + //node1 is the head + node1.next = Some(node2) + node2.next = Some(node3) + node3.next = Some(node4) + node4.next = Some(node5) + node5.next = Some(node6) + node6.next = Some(node3) + + assert(LinkedListAlgo.checkCircle(node1)) + } + + it should "check circle for none circled linked list" in { + + val node1 = new Node(1, None) + val node2 = new Node(2, None) + val node3 = new Node(3, None) + val node4 = new Node(4, None) + val node5 = new Node(5, None) + val node6 = new Node(6, None) + + //1->2->3->4->5->6->null it's not a circle + //node1 is the head + node1.next = Some(node2) + node2.next = Some(node3) + node3.next = Some(node4) + node4.next = Some(node5) + node5.next = Some(node6) + + assert(!LinkedListAlgo.checkCircle(node1)) + } + +}