베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • C++
    • 이펙티브자바
    • 데이터베이스
    • 자바
    • 운영체제
    • 토비스프링
    • 스프링시큐리티
    • 스프링
    • 스레드
    • 코드업
    • java
    • 백준
    • 자바8
    • jpa
    • leetcode
    • Spring
    • 함수형인터페이스
    • BFS
    • dfs
    • 알고리즘

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/LeetCode

    [LeetCode] Reverse Linked List (Java)

    2023. 7. 20. 12:38

    ✉️문제

    https://leetcode.com/problems/reverse-linked-list/description/

     

    Reverse Linked List - LeetCode

    Can you solve this real interview question? Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list.   Example 1: [https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg] Input: head = [1,2,3,4,5] O

    leetcode.com

     

    🗝 문제풀이

    public class ReverseLinkedList {
        
        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; }
       }
    
    	// 스택을 이용한 방법
        public ListNode reverseList(ListNode head) {
            if(head == null) return null;
    
            ListNode root = head;
            Stack<ListNode> stack = new Stack<>();
            while(head != null) {
                stack.push(head);
                head = head.next;
            }
    
            ListNode res = stack.pop();
            ListNode answer = res;
            while(!stack.isEmpty()) {
                ListNode tmp = stack.pop();
                tmp.next = null;
                res.next = tmp;
                res = res.next;
            }
    
            return answer;
        }
    
    	// 포인터를 이용한 방법
        public ListNode reverseList2(ListNode head) {
            ListNode prev = null;
            ListNode current = head;
    
            while(current != null) {
                ListNode next = current.next;
                current.next = prev;
                prev = current;
                current = next;
            }
    
            return prev;
        }
    }
    저작자표시 비영리 변경금지 (새창열림)
      '알고리즘/LeetCode' 카테고리의 다른 글
      • [LeetCode] Merge Two Sorted Lists (Java)
      • [LeetCode] Linked List Cycle (Java)
      • [LeetCode] Longest Consecutive Sequence (Java)
      • [LeetCode] House Robber II (Java)
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바