Skip to content

Commit

Permalink
reverse linked list solved
Browse files Browse the repository at this point in the history
  • Loading branch information
mintheon committed Jan 23, 2025
1 parent 03adfe9 commit 4dfafee
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions reverse-linked-list/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
// 실행시간: O(n)
// 공간복잡도: O(1)
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode cur = head;

while(cur != null) {
ListNode nextTemp = cur.next;
cur.next = prev;
prev = cur;
cur = nextTemp;
}

return prev;
}
}

0 comments on commit 4dfafee

Please sign in to comment.