Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 더현대서울 맛집
- 자연어처리
- 데이콘
- Kaggle
- 우분투
- Baekjoon
- 백준
- Git
- Docker
- 프로그래머스
- 코로나19
- 맥북
- ChatGPT
- 프로그래머스 파이썬
- PYTHON
- leetcode
- 편스토랑
- programmers
- 금융문자분석경진대회
- SW Expert Academy
- 캐치카페
- 편스토랑 우승상품
- dacon
- hackerrank
- Real or Not? NLP with Disaster Tweets
- 파이썬
- github
- ubuntu
- gs25
- AI 경진대회
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 206. Reverse Linked List (Python) 본문
728x90
반응형
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Solution
# 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:
my_list = []
answerList = None
while head != None:
val = head.val
my_list.append(val)
head = head.next
my_list_reverse = my_list[::-1]
for i in range(len(my_list_reverse)):
if i == 0:
answerList = ListNode(my_list_reverse[i])
else:
new_node = ListNode(my_list_reverse[i])
currNode = answerList
while currNode.next != None:
currNode = currNode.next
currNode.next = new_node
return answerList
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 27. Remove Element (Python) (0) | 2020.10.24 |
---|---|
[leetCode] 216. Combination Sum III (Python) (0) | 2020.10.23 |
[leetCode] 1078. Occurrences After Bigram (Python) (0) | 2020.10.21 |
[leetCode] 451. Sort Characters By Frequency (Python) (0) | 2020.10.20 |
[leetCode] 215. Kth Largest Element in an Array (Python) (0) | 2020.10.19 |
Comments