Skip to content

Commit 75361b7

Browse files
author
Jeongwon Na
committed
solution: reverse-linked-list using iteration
1 parent 5bc4b26 commit 75361b7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

reverse-linked-list/njngwn.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
// Time Complexity: O(n)
12+
// Sprace Compexity: O(1)
13+
class Solution {
14+
public ListNode reverseList(ListNode head) {
15+
ListNode prevNode = null;
16+
ListNode currNode = head;
17+
18+
while (currNode != null) {
19+
ListNode nextNode = currNode.next;
20+
currNode.next = prevNode;
21+
prevNode = currNode;
22+
currNode = nextNode;
23+
}
24+
25+
return prevNode;
26+
}
27+
}

0 commit comments

Comments
 (0)