diff --git a/animation-simulation/栈和队列/225.用队列实现栈.md b/animation-simulation/栈和队列/225.用队列实现栈.md index c6b17c8..ccaa942 100644 --- a/animation-simulation/栈和队列/225.用队列实现栈.md +++ b/animation-simulation/栈和队列/225.用队列实现栈.md @@ -18,6 +18,7 @@ 下面我们来看一下题目代码,也是很容易理解。 +Java Code: ```java class MyStack { //初始化队列 @@ -55,3 +56,34 @@ class MyStack { ``` +JS Code: +```javascript +var MyStack = function() { + this.queue = []; +}; + +MyStack.prototype.push = function(x) { + this.queue.push(x); + if (this.queue.length > 1) { + let i = this.queue.length - 1; + while (i) { + this.queue.push(this.queue.shift()); + i--; + } + } +}; + +MyStack.prototype.pop = function() { + return this.queue.shift(); +}; + +MyStack.prototype.top = function() { + return this.empty() ? null : this.queue[0]; + +}; + +MyStack.prototype.empty = function() { + return !this.queue.length; +}; +``` + diff --git a/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md b/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md index 5ac5c34..63d9c10 100644 --- a/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md +++ b/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md @@ -58,6 +58,7 @@ class CQueue { 大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。 +Java Code: ```java class CQueue { //初始化两个栈 @@ -89,3 +90,24 @@ class CQueue { } ``` +JS Code: +```javascript +var CQueue = function() { + this.stack1 = []; + this.stack2 = []; +}; + +CQueue.prototype.appendTail = function(value) { + this.stack1.push(value); +}; + +CQueue.prototype.deleteHead = function() { + if (!this.stack2.length) { + while(this.stack1.length) { + this.stack2.push(this.stack1.pop()); + } + } + return this.stack2.pop() || -1; +}; +``` + diff --git a/animation-simulation/链表篇/leetcode141环形链表.md b/animation-simulation/链表篇/leetcode141环形链表.md index 528d421..d157030 100644 --- a/animation-simulation/链表篇/leetcode141环形链表.md +++ b/animation-simulation/链表篇/leetcode141环形链表.md @@ -38,6 +38,7 @@ **题目代码** +Java Code: ```java public class Solution { public boolean hasCycle(ListNode head) { @@ -56,3 +57,18 @@ public class Solution { } ``` +JS Code: +```javascript +var hasCycle = function(head) { + let fast = head; + let slow = head; + while (fast && fast.next) { + fast = fast.next.next; + slow = slow.next; + if (fast === slow) { + return true; + } + } + return false; +}; +``` diff --git a/animation-simulation/链表篇/leetcode206反转链表.md b/animation-simulation/链表篇/leetcode206反转链表.md index 3130b04..93b6261 100644 --- a/animation-simulation/链表篇/leetcode206反转链表.md +++ b/animation-simulation/链表篇/leetcode206反转链表.md @@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成 我会对每个关键点进行注释,大家可以参考动图理解。 +Java Code: ```java class Solution { public ListNode reverseList(ListNode head) { @@ -62,9 +63,27 @@ class Solution { } ``` +JS Code: +```javascript +var reverseList = function(head) { + if(!head || !head.next) { + return head; + } + let low = null; + let pro = head; + while (pro) { + let temp = pro; + pro = pro.next; + temp.next = low; + low = temp; + } + return low; +}; +``` 上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。 +Java Code: ```java class Solution { public ListNode reverseList(ListNode head) { @@ -86,3 +105,16 @@ class Solution { ``` +JS Code: +```javascript +var reverseList = function(head) { + if (!head || !head.next) { + return head; + } + let pro = reverseList(head.next); + head.next.next = head; + head.next = null; + return pro; +}; +``` +