Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ffaa0d

Browse files
committed
Refine 206_Reverse_Linked_List
1 parent b516688 commit 7ffaa0d

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
8585
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/198_House_Robber.py) | f(k) = max(f(k – 2) + num[k], f(k – 1)), O(n) and O(1) |
8686
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/200_Number_of_Islands.py) | 1. Quick union find, O(nlogn) and O(n^2)<br>2. BFS with marks, O(n^2) and O(1) |
8787
| 204 | [Count Primes](https://leetcode.com/problems/count-primes/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/204_Count_Primes.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/204_Count_Primes.java) [CPP](https://github.com/qiyuangong/leetcode/blob/master/cpp/204_Count_Primes.cpp) | [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_complexity), O(nloglogn) and O(n) |
88-
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/206_Reverse_Linked_List.py) | 1. Stack, O(n) and O(n)<br>2. Traverse on prev and curr, then curr.next = prev, O(n) and O(1)<br>3. Recursion, O(n) and O(1) |
88+
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/206_Reverse_Linked_List.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/206_Reverse_Linked_List.java) [CPP](https://github.com/qiyuangong/leetcode/blob/master/cpp/206_Reverse_Linked_List.cpp) | 1. Stack, O(n) and O(n)<br>2. Traverse on prev and curr, then curr.next = prev, O(n) and O(1)<br>3. Recursion, O(n) and O(1) |
8989
| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/213_House_Robber_II.py) | f(k) = max(f(k – 2) + num[k], max(dp[0~ls-2],dp[1~ls-1], O(n) and O(1)|
9090
| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/215_Kth_Largest_Element_in_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/215_Kth_Largest_Element_in_an_Array.java) | 1. Sort, O(n) and O(n)<br>2. Heap, O(nlgk) and O(n)<br>3. Quick selection, O(klgn) and O(n)|
9191
| 216 | [Combination Sum III](https://leetcode.com/problems/combination-sum-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/216_Combination_Sum_III.py) | Generate all combinations of length k and keep those that sum to n|

cpp/206_Reverse_Linked_List.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
*/
99
class Solution {
1010
public:
11-
ListNode* reverseList(ListNode* head) {
12-
if(head==NULL)return NULL;
13-
if(head->next==NULL) return head;
14-
ListNode* p=head;
15-
ListNode* c=head->next;
16-
head->next=NULL;
17-
while(c->next){
18-
ListNode* n = c->next;
19-
c->next=p;
20-
p=c;
21-
c=n;
11+
ListNode *reverseList(ListNode *head) {
12+
if (head == NULL)
13+
return NULL;
14+
if (head->next == NULL)
15+
return head;
16+
// Previous pointer
17+
ListNode *previous = head;
18+
// Current pointer
19+
ListNode *curr = head->next;
20+
head->next = NULL;
21+
while (curr->next) {
22+
ListNode *next = curr->next;
23+
curr->next = previous;
24+
previous = curr;
25+
curr = next;
2226
}
23-
c->next=p;
24-
return c;
27+
curr->next = previous;
28+
return curr;
2529
}
2630
};

java/206_Reverse_Linked_List.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
// https://leetcode.com/problems/reverse-linked-list/discuss/58125/In-place-iterative-and-recursive-Java-solution
13+
public ListNode reverseList(ListNode head) {
14+
// Iterative solution
15+
ListNode newHead = null;
16+
while (head != null) {
17+
ListNode next = head.next;
18+
head.next = newHead;
19+
newHead = head;
20+
head = next;
21+
}
22+
return newHead;
23+
}
24+
25+
// Recursive
26+
/*
27+
public ListNode reverseList(ListNode head) {
28+
return reverseListInt(head, null);
29+
}
30+
31+
private ListNode reverseListInt(ListNode head, ListNode newHead) {
32+
if (head == null)
33+
return newHead;
34+
ListNode next = head.next;
35+
head.next = newHead;
36+
return reverseListInt(next, head);
37+
}*/
38+
}

0 commit comments

Comments
 (0)