Skip to content

Commit 6c4dc21

Browse files
solves reverse linked list
1 parent 59a0a31 commit 6c4dc21

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
| 203 | [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/RemoveLinkedListElements.java) |
6565
| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/CountPrimes.java) |
6666
| 205 | [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IsomorphicStrings.java) |
67-
| 206 | [Reverse Linked Lists](https://leetcode.com/problems/reverse-linked-list) | Easy |
67+
| 206 | [Reverse Linked Lists](https://leetcode.com/problems/reverse-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)]() |
6868
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | Easy |
6969
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii) | Easy |
7070
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues) | Easy |

src/ReverseLinkedList.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class ReverseLinkedList {
2+
private static class ListNode {
3+
int val;
4+
ListNode next;
5+
}
6+
7+
public static ListNode reverseList(ListNode head) {
8+
if (head == null || head.next == null) {
9+
return head;
10+
}
11+
12+
ListNode current = head, ahead = head.next, temp = head.next.next;
13+
head.next = null;
14+
15+
while (temp != null) {
16+
ahead.next = current;
17+
current = ahead;
18+
ahead = temp;
19+
temp = temp.next;
20+
}
21+
ahead.next = current;
22+
return ahead;
23+
}
24+
}

0 commit comments

Comments
 (0)