8000 Sync LeetCode submission - Reverse Linked List (java) · thatbeautifuldream/leetcode-sync@bf22a60 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf22a60

Browse files
Sync LeetCode 8000 submission - Reverse Linked List (java)
1 parent 179e561 commit bf22a60

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
public ListNode reverseList(ListNode head) {
13+
ListNode prev = null;
14+
while(head != null) {
15+
ListNode next = head.next;
16+
head.next = prev;
17+
prev = head;
18+
head = next;
19+
}
20+
return prev;
21+
}
22+
}

0 commit comments

Comments
 (0)
0