Skip to content

Commit 019027c

Browse files
committed
add reverse linked list solution
1 parent 80151b3 commit 019027c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

reverse-linked-list/Tessa1217.java

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+
* 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+
/**
12+
* 링크드 리스트 head가 주어질 때 뒤집은 리스트를 반환하세요.
13+
*/
14+
class Solution {
15+
public ListNode reverseList(ListNode head) {
16+
17+
if (head == null) {
18+
return head;
19+
}
20+
21+
ListNode prev = head;
22+
ListNode next = prev.next;
23+
head.next = null;
24+
25+
while (next != null) {
26+
ListNode temp = next.next;
27+
next.next = prev;
28+
prev = next;
29+
next = temp;
30+
}
31+
32+
return prev;
33+
}
34+
35+
}
36+

0 commit comments

Comments
 (0)