베어_
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
    • 백준
    • 데이터베이스
    • 스레드
    • 스프링
    • 코드업
    • 스프링시큐리티
    • 이펙티브자바
    • leetcode
    • BFS
    • 자바
    • dfs
    • 자바8
    • Spring
    • jpa

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/LeetCode

    [LeetCode] Merge Two Sorted Lists (Java)

    2023. 7. 20. 16:49

    ✉️문제

    https://leetcode.com/problems/merge-two-sorted-lists/description

     

    Merge Two Sorted Lists - LeetCode

    Can you solve this real interview question? Merge Two Sorted Lists - You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists

    leetcode.com

     

    🗝 문제풀이

      public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
            ListNode prehead = new ListNode(-1);
            ListNode cur = prehead;
    
            while(list1 != null && list2 != null) {
                if(list1.val >= list2.val) {
                    cur.next = list2;
                    list2 = list2.next;
                } else {
                    cur.next = list1;
                    list1 = list1.next;
                }
                cur = cur.next;
            }
    
            cur.next = (list1 == null ? list2 : list1);
            return prehead.next;
        }
    저작자표시 비영리 변경금지 (새창열림)
      '알고리즘/LeetCode' 카테고리의 다른 글
      • [LeetCode] Insert Interval
      • [LeetCode] Reorder List (Java)
      • [LeetCode] Linked List Cycle (Java)
      • [LeetCode] Reverse Linked List (Java)
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바