Skip to content

Commit ef210d8

Browse files
committed
add vscode dir && solve 206
1 parent 44b9cae commit ef210d8

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

vscode/206.reverse-linked-list.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode id=206 lang=java
3+
*
4+
* [206] Reverse Linked List
5+
*
6+
* https://leetcode.com/problems/reverse-linked-list/description/
7+
*
8+
* algorithms
9+
* Easy (52.81%)
10+
* Total Accepted: 520.1K
11+
* Total Submissions: 984.7K
12+
* Testcase Example: '[1,2,3,4,5]'
13+
*
14+
* Reverse a singly linked list.
15+
*
16+
* Example:
17+
*
18+
*
19+
* Input: 1->2->3->4->5->NULL
20+
* Output: 5->4->3->2->1->NULL
21+
*
22+
*
23+
* Follow up:
24+
*
25+
* A linked list can be reversed either iteratively or recursively. Could you
26+
* implement both?
27+
*
28+
*/
29+
/**
30+
* Definition for singly-linked list. public class ListNode { int val; ListNode
31+
* next; ListNode(int x) { val = x; } }
32+
*/
33+
class Solution {
34+
public ListNode reverseList(ListNode head) {
35+
ListNode prev = null;
36+
ListNode curr = head;
37+
while (curr != null) {
38+
ListNode next = curr.next;
39+
curr.next = prev;
40+
prev = curr;
41+
curr = next;
42+
}
43+
return prev;
44+
}
45+
}

0 commit comments

Comments
 (0)