Skip to content

Commit

Permalink
reversed linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
eunhwa99 committed Jan 18, 2025
1 parent a26f8ee commit 21586c4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions reverse-linked-list/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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(N)
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null) return head;

ListNode pointer = new ListNode(head.val);

ListNode tempPointer;
while(head.next!=null){
tempPointer = new ListNode(head.next.val, pointer);
pointer = tempPointer;
head = head.next;
}
}
return pointer;
}

0 comments on commit 21586c4

Please sign in to comment.