From 51de6228dbbf95a238270097982ba4a95bbe2145 Mon Sep 17 00:00:00 2001 From: Ulas Aydin Date: Fri, 4 Oct 2019 00:19:40 +0300 Subject: [PATCH 1/3] Added reverse-linked-list problem --- README.md | 1 + .../LinkedList/reverse-linked-list/index.js | 15 ++++++++ .../reverse-linked-list.test.js | 35 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/_DataStructures_/LinkedList/reverse-linked-list/index.js create mode 100644 src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js diff --git a/README.md b/README.md index 90873021..02f38250 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [N Element From Last](src/_DataStructures_/LinkedList/element-from-last) - [Middle Node](src/_DataStructures_/LinkedList/middle-node) - [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list) + - [Reverse Linked List](src/_DataStructures_/LinkedList/reverse-linked-list) - [Stack](src/_DataStructures_/Stack) - [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack) - [Baseball Game](src/_DataStructures_/Stack/baseball-game) diff --git a/src/_DataStructures_/LinkedList/reverse-linked-list/index.js b/src/_DataStructures_/LinkedList/reverse-linked-list/index.js new file mode 100644 index 00000000..02886e1b --- /dev/null +++ b/src/_DataStructures_/LinkedList/reverse-linked-list/index.js @@ -0,0 +1,15 @@ +function reverseLinkedList(linkedList) { + var next = linkedList.getFirst(); + var current = null; + var prev; + while(next != null){ + prev = current; + current = next; + next = next.next; + current.next = prev; + } + return current; +}; +module.exports = { + reverseLinkedList, + }; \ No newline at end of file diff --git a/src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js b/src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js new file mode 100644 index 00000000..69244ca6 --- /dev/null +++ b/src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js @@ -0,0 +1,35 @@ +const { LinkedList } = require('../index'); +const { reverseLinkedList } = require('.').default; + +describe('Reverse a LinkedList', () => { + let list = null; + beforeEach(() => { + list = new LinkedList(); + list.addAtBeginning('1'); + list.addAtEnd('2'); + list.addAtEnd('3'); + list.addAtEnd('4'); + list.addAtEnd('5'); + }); + + it('Should return `null` for empty list', () => { + list.delete(); + expect(reverseLinkedList(list)).toEqual(null); + }); + + it('Should return `5`->`4`->`3`->`2`->`1` for the given list', () => { + expect(reverseLinkedList(list).data).toEqual('5'); + expect(reverseLinkedList(list).next.data).toEqual('4'); + expect(reverseLinkedList(list).next.next.data).toEqual('3'); + expect(reverseLinkedList(list).next.next.next.data).toEqual('2'); + expect(reverseLinkedList(list).next.next.next.next.data).toEqual('1'); + }); + + it('Should return `3`->`2`->`1` after deleting 2 last nodes of the list', () => { + list.removeFromEnd(); + list.removeFromEnd(); + expect(reverseLinkedList(list).data).toEqual('3'); + expect(reverseLinkedList(list).next.data).toEqual('2'); + expect(reverseLinkedList(list).next.next.data).toEqual('1'); + }); +}); From 7646ad02d118365403c8f3ca8a7aa692426b966f Mon Sep 17 00:00:00 2001 From: Ulas Aydin Date: Fri, 4 Oct 2019 01:01:56 +0300 Subject: [PATCH 2/3] Fixed reverseLinkedList testing bug --- .../reverse-linked-list.test.js | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js b/src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js index 69244ca6..eba6c5d8 100644 --- a/src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js +++ b/src/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js @@ -1,5 +1,5 @@ const { LinkedList } = require('../index'); -const { reverseLinkedList } = require('.').default; +const { reverseLinkedList } = require('.'); describe('Reverse a LinkedList', () => { let list = null; @@ -18,18 +18,20 @@ describe('Reverse a LinkedList', () => { }); it('Should return `5`->`4`->`3`->`2`->`1` for the given list', () => { - expect(reverseLinkedList(list).data).toEqual('5'); - expect(reverseLinkedList(list).next.data).toEqual('4'); - expect(reverseLinkedList(list).next.next.data).toEqual('3'); - expect(reverseLinkedList(list).next.next.next.data).toEqual('2'); - expect(reverseLinkedList(list).next.next.next.next.data).toEqual('1'); + let reversedList = reverseLinkedList(list); + expect(reversedList.data).toEqual('5'); + expect(reversedList.next.data).toEqual('4'); + expect(reversedList.next.next.data).toEqual('3'); + expect(reversedList.next.next.next.data).toEqual('2'); + expect(reversedList.next.next.next.next.data).toEqual('1'); }); it('Should return `3`->`2`->`1` after deleting 2 last nodes of the list', () => { list.removeFromEnd(); list.removeFromEnd(); - expect(reverseLinkedList(list).data).toEqual('3'); - expect(reverseLinkedList(list).next.data).toEqual('2'); - expect(reverseLinkedList(list).next.next.data).toEqual('1'); + let reversedList2 = reverseLinkedList(list); + expect(reversedList2.data).toEqual('3'); + expect(reversedList2.next.data).toEqual('2'); + expect(reversedList2.next.next.data).toEqual('1'); }); }); From dd2c796d6cf6280f350f4d8085049fbe72a7f1c4 Mon Sep 17 00:00:00 2001 From: ulasaydin <32299119+ulasaydin@users.noreply.github.com> Date: Fri, 4 Oct 2019 15:32:09 +0300 Subject: [PATCH 3/3] Update index.js 'var's are replaced with 'let's. --- .../LinkedList/reverse-linked-list/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_DataStructures_/LinkedList/reverse-linked-list/index.js b/src/_DataStructures_/LinkedList/reverse-linked-list/index.js index 02886e1b..11dd4d50 100644 --- a/src/_DataStructures_/LinkedList/reverse-linked-list/index.js +++ b/src/_DataStructures_/LinkedList/reverse-linked-list/index.js @@ -1,7 +1,7 @@ function reverseLinkedList(linkedList) { - var next = linkedList.getFirst(); - var current = null; - var prev; + let next = linkedList.getFirst(); + let current = null; + let prev; while(next != null){ prev = current; current = next; @@ -12,4 +12,4 @@ function reverseLinkedList(linkedList) { }; module.exports = { reverseLinkedList, - }; \ No newline at end of file + };