Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7be6b4b

Browse files
committed
add 92
1 parent403abae commit7be6b4b

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

‎Easy/206-reversedLinkedList.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
// Iterative way
1313
varreverseList=function(head){
14-
if(head===null||head.next===null)returnhead;
14+
if(head===null||head.next===null)returnhead;
1515
varheadNext=head.next;
1616
head.next=null;
1717
while(head!==null&&headNext!==null){
@@ -25,6 +25,18 @@ var reverseList = function(head) {
2525
returnhead;
2626
};
2727

28+
// a more concise solution, prevHead eventually wll be the reversed list head
29+
varreverseList=function(head){
30+
varprevHead=null;
31+
while(head){
32+
varheadNext=head.next;
33+
head.next=prevHead;
34+
prevHead=head;
35+
head=headNext;
36+
}
37+
returnprevHead;
38+
};
39+
2840
// recursive way
2941
varreverseList=function(head){
3042
if(head===null||head.next===null)returnhead;

‎Medium/92-reverseLinkedListII.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
*@param {ListNode} head
10+
*@param {number} m
11+
*@param {number} n
12+
*@return {ListNode}
13+
*/
14+
varreverseBetween=function(head,m,n){
15+
vardummyHead=newListNode(null);
16+
dummyHead.next=head;
17+
varpre=dummyHead;
18+
19+
for(vari=1;i<m;i++){
20+
pre=pre.next;
21+
}
22+
23+
varcurr=pre.next;
24+
varnewHead=pre;
25+
for(vari=0;i<=n-m;i++){
26+
varcurrNext=curr.next;
27+
curr.next=pre;
28+
pre=curr;
29+
curr=currNext;
30+
}
31+
// reset the head and tail pointers
32+
newHead.next.next=curr;
33+
newHead.next=pre;
34+
35+
returndummyHead.next;
36+
};

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
*[77.Combinations](https://leetcode.com/problems/combinations/) -[Solution](./Medium/77-combinations.js)
9595
*[80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) -[Solution](./Medium/80-removeDuplicatesII.js)
9696
*[89. Gray Code](https://leetcode.com/problems/gray-code/) -[Solution](./Medium/89-grayCode.js)
97+
*[92. Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) -[Solution](./Medium/92-reverseLinkedListII.js)
9798
*[94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) -[Solution](./Medium/94-binaryTreeInorder.js)
9899
*[96. Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) -[Solution](./Medium/96-uniqueBinarySearchTrees.js)
99100
*[108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) -[Solution](./Medium/108-convertSortedArraytoBST.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp