[코테 풀이] Reverse Linked List

시내·2024년 6월 9일
0

Q_206) Reverse Linked List

출처 : https://leetcode.com/problems/reverse-linked-list/?envType=study-plan-v2&envId=programming-skills

Given the head of a singly linked list, reverse the list, and return the reversed list.

/**
 * 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; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head)  {
        ListNode previous = null;
        ListNode current = head;

        while (current != null) {
            ListNode next = current.next;
            current.next = previous;
            previous = current;
            current = next;
        }
        return previous;
    }
}

🙈 풀이 참조한 문제

출처 : https://leetcode.com/problems/reverse-linked-list/discuss/2682085/JAVA-0ms-100-Easy-Understanding

profile
contact 📨 ksw08215@gmail.com

0개의 댓글

관련 채용 정보