Skip to content

Commit 4f4ad89

Browse files
author
Jinbeom
committed
Reverse Linked List Solution
1 parent 97dfab3 commit 4f4ad89

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

reverse-linked-list/kayden.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// 시간복잡도: O(N)
12+
// 공간복잡도: O(1)
13+
class Solution {
14+
public ListNode reverseList(ListNode head) {
15+
16+
ListNode prev = null;
17+
18+
while (head != null){
19+
ListNode curr = new ListNode(head.val, prev);
20+
prev = curr;
21+
head = head.next;
22+
}
23+
24+
return prev;
25+
}
26+
}

0 commit comments

Comments
 (0)