Skip to content

Commit fd4d875

Browse files
donghyeon95donghyeon95
authored andcommitted
feat: Reverse Linked List #223
1 parent 2325e53 commit fd4d875

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// 시간 복잡도 : O(n)
13+
// 공간 복잡도 : O(n)
14+
class Solution {
15+
public ListNode reverseList(ListNode head) {
16+
// 반복문을 돌면서 f(x+1)의 next를 f(x)로 지정
17+
ListNode result = null;
18+
19+
while(head != null) {
20+
ListNode nextNode = new ListNode(head.val);
21+
nextNode.next = result;
22+
result = nextNode;
23+
head = head.next;
24+
}
25+
26+
return result;
27+
}
28+
}

0 commit comments

Comments
 (0)