문제 링크 : leetcode.com/problems/reverse-linked-list/

 

Reverse Linked List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

이 문제는 linked list를 역순으로 반환하는 문제이다.

 

풀이방법

새로운 linked list를 만들고, 입력받은 linked list를 탐색하면서 새로운 linked list를 역순으로 연결되도록 하였다.

 

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        prev = None # 첫 Linked List 생성
        # head List의 끝까지 탐색
        while head:
            temp = ListNode()
            temp.val = head.val
            temp.next = prev
            prev = temp
            head = head.next
        return prev

 

'Algorithm > Leetcode' 카테고리의 다른 글

[LeetCode] 328. Odd Even Linked List  (0) 2021.01.24
[LeetCode] 24. Swap Nodes in Pairs  (0) 2021.01.24
[LeetCode] 2. Add Two Numbers  (0) 2021.01.23
[LeetCode] 21. Merge Two Sorted Lists  (0) 2021.01.23
[LeetCode] 234. Palindrome Linked List  (0) 2021.01.23

+ Recent posts