Skip to content

Commit e052711

Browse files
committed
add: Reverse Integer and Remove Nth Node From End of List
1 parent 5e95964 commit e052711

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Progress: 20/
1010
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [JavaScript](./src/add-two-numbers/res.js)|Medium|
1111
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [JavaScript](./src/reverse-integer/res.js)|Easy|
1212
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [JavaScript](./src/roman-to-integer/res.js)|Easy|
13+
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [JavaScript](./src/remove-nth-node-from-end-of-list/res.js)|Medium|
1314
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [JavaScript](./src/generate-parentheses/res.js)|Medium|
1415
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [JavaScript](./src/remove-duplicates-from-sorted-array/res.js)|Easy|
1516
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
@@ -21,6 +22,7 @@ Progress: 20/
2122
|182|[Duplicate Emails](https://leetcode.com/problems/duplicate-emails/) | [SQL](./src/duplicate-emails/res.txt)|Easy|
2223
|183|[Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/) | [SQL](./src/customers-who-never-order/res.txt)|Easy|
2324
|184|[Department Highest Salary](https://leetcode.com/problems/department-highest-salary/) | [SQL](./src/department-highest-salary/res.txt)|Medium|
25+
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [JavaScript](./src/reverse-bits/res.txt)|Easy|
2426
|196|[Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/) | [SQL](./src/delete-duplicate-emails/res.txt)|Easy|
2527
|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/) | [SQL](./src/rising-temperature/res.txt)|Easy|
2628
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | [JavaScript](./src/power-of-four/res.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Given a linked list, remove the nth node from the end of list and return its head.
2+
3+
// For example,
4+
5+
// Given linked list: 1->2->3->4->5, and n = 2.
6+
7+
// After removing the second node from the end, the linked list becomes 1->2->3->5.
8+
// Note:
9+
// Given n will always be valid.
10+
// Try to do this in one pass.
11+
12+
/**
13+
* res.js
14+
* @authors Joe Jiang (hijiangtao@gmail.com)
15+
* @date 2017-02-25 18:17:02
16+
* @version $Id$
17+
*/
18+
19+
/**
20+
* Definition for singly-linked list.
21+
* function ListNode(val) {
22+
* this.val = val;
23+
* this.next = null;
24+
* }
25+
*/
26+
/**
27+
* @param {ListNode} head
28+
* @param {number} n
29+
* @return {ListNode}
30+
*/
31+
let removeNthFromEnd = function(head, n) {
32+
let fast = head,
33+
slow = head;
34+
35+
for (let i=0; i<n; i++) {
36+
fast = fast.next;
37+
}
38+
39+
if (!fast) {
40+
return head.next;
41+
}
42+
43+
while(fast.next) {
44+
fast = fast.next;
45+
slow = slow.next;
46+
}
47+
48+
slow.next = slow.next.next;
49+
return head;
50+
};

src/reverse-bits/res.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Reverse bits of a given 32 bits unsigned integer.
2+
3+
// For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
4+
5+
// Follow up:
6+
// If this function is called many times, how would you optimize it?
7+
8+
/**
9+
* res.js
10+
* @authors Joe Jiang (hijiangtao@gmail.com)
11+
* @date 2017-02-25 20:55:29
12+
* @version $Id$
13+
*/
14+
15+
/**
16+
* @param {number} n - a positive integer
17+
* @return {number} - a positive integer
18+
*/
19+
var reverseBits = function(n) {
20+
let bits = 31,
21+
divs = Math.pow(2, 31),
22+
res = 0;
23+
24+
for ( ; n !== 0; ) {
25+
let judge = Math.floor(n/divs);
26+
if (judge) {
27+
res += Math.pow(2, 31-bits);
28+
}
29+
n = n%divs;
30+
divs /= 2;
31+
bits -= 1;
32+
}
33+
34+
return res;
35+
};

0 commit comments

Comments
 (0)