From 94b2c6826736363a03443aa905ae051dda030169 Mon Sep 17 00:00:00 2001
From: beizhedenglong <2501211450@qq.com>
Date: Sun, 11 Sep 2016 23:26:57 +0800
Subject: [PATCH] fix bug in CircularLinkedList

There is a bug in insert function. If you insert an element in an empty
list at the first  position. It will throw a TypeError: Cannot read
property 'next' of null.
---
 chapter05/05-CircularLinkedList.js  | 27 +++++++++++++++++----------
 chapter05/05-CircularLinkedList2.js | 21 ++++++++++++---------
 2 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/chapter05/05-CircularLinkedList.js b/chapter05/05-CircularLinkedList.js
index 5f05e85f..c3151148 100644
--- a/chapter05/05-CircularLinkedList.js
+++ b/chapter05/05-CircularLinkedList.js
@@ -46,16 +46,22 @@ function CircularLinkedList() {
                 index = 0;
 
             if (position === 0){ //add on first position
-
-                node.next = current;
-
-                //update last element
-                while(current.next !== head){ //last element will be head instead of NULL
-                    current = current.next;
+                
+                if(!head){ // if no node  in list
+                    head = node;
+                    node.next = head;
+                }else{
+                    node.next = current;
+
+                    //update last element
+                    while(current.next !== head){ //last element will be head instead of NULL
+                        current = current.next;
+                    }
+
+                    head = node;
+                    current.next = head;
                 }
-
-                head = node;
-                current.next = head;
+                
 
             } else {
                 while (index++ < position){
@@ -180,4 +186,5 @@ function CircularLinkedList() {
     this.print = function(){
         console.log(this.toString());
     };
-}
\ No newline at end of file
+}
+
diff --git a/chapter05/05-CircularLinkedList2.js b/chapter05/05-CircularLinkedList2.js
index 5aab62d0..6f7574ef 100644
--- a/chapter05/05-CircularLinkedList2.js
+++ b/chapter05/05-CircularLinkedList2.js
@@ -58,15 +58,18 @@ let CircularLinkedList2 = (function () {
 
               if (position === 0) { //add on first position
 
-                    node.next = current;
-
-                    //update last element
-                    while (current.next !== this.getHead()) { //last element will be head instead of NULL
-                        current = current.next;
-                    }
-
-                    head.set(this, node);
-                    current.next = this.getHead();
+                  if(!this.getHead()) { // if no node  in list
+                      head.set(this, node);
+                      node.next = this.getHead();
+                  } else {
+                      node.next = current;
+                      //update last element
+                      while(current.next !== this.getHead()) { //last element will be head instead of NULL
+                          current = current.next;
+                      }
+                      head.set(this, node);
+                      current.next = this.getHead();
+                  }
 
                 } else {
                     while (index++ < position) {