We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2325e53 commit fd4d875Copy full SHA for fd4d875
reverse-linked-list/donghyeon95.java
@@ -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