일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 프로그래머스
- 우분투
- 데이콘
- 자연어처리
- 금융문자분석경진대회
- 캐치카페
- 더현대서울 맛집
- dacon
- SW Expert Academy
- gs25
- PYTHON
- ChatGPT
- 코로나19
- Real or Not? NLP with Disaster Tweets
- leetcode
- Baekjoon
- github
- Kaggle
- 편스토랑
- hackerrank
- Docker
- Git
- ubuntu
- 프로그래머스 파이썬
- 파이썬
- AI 경진대회
- programmers
- 백준
- 맥북
- 편스토랑 우승상품
- Today
- Total
솜씨좋은장씨
[HackerRank] Reverse a doubly linked list (Python) 본문
Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list.
Note: The head node might be NULL to indicate that the list is empty.
Function Description
Complete the reverse function in the editor below.
reverse has the following parameter(s):
- DoublyLinkedListNode head: a reference to the head of a DoublyLinkedList
Returns
- DoublyLinkedListNode: a reference to the head of the reversed list
Input Format
The first line contains an integer t, the number of test cases.
Each test case is of the following format:
- The first line contains an integer n, the number of elements in the linked list.
- The next n lines contain an integer each denoting an element of the linked list.
Constraints
- 1 <= t <= 10
- 0 <= n <= 1000
- 0 <= DoublyLinkedListNode.data <= 1000
Output Format
Return a reference to the head of your reversed list. The provided code will print the reverse array as a one line of space-separated integers for each test case.
Sample Input
1
4
1
2
3
4
Sample Output
4 3 2 1
Explanation
The initial doubly linked list is: 1 <-> 2 <-> 3 <-> 4 -> NULL
The reversed doubly linked list is: 4 <-> 3 <-> 2 <-> 1 -> NULL
Solution
#!/bin/python3
import math
import os
import random
import re
import sys
class DoublyLinkedListNode:
def __init__(self, node_data):
self.data = node_data
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def insert_node(self, node_data):
node = DoublyLinkedListNode(node_data)
if not self.head:
self.head = node
else:
self.tail.next = node
node.prev = self.tail
self.tail = node
def print_doubly_linked_list(node, sep, fptr):
while node:
fptr.write(str(node.data))
node = node.next
if node:
fptr.write(sep)
# Complete the reverse function below.
#
# For your reference:
#
# DoublyLinkedListNode:
# int data
# DoublyLinkedListNode next
# DoublyLinkedListNode prev
#
#
def reverse(head):
prev = None
d_node = head
while d_node != None:
temp = d_node.next
d_node.next = prev
prev = d_node
d_node = temp
head = prev
return head
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
llist_count = int(input())
llist = DoublyLinkedList()
for _ in range(llist_count):
llist_item = int(input())
llist.insert_node(llist_item)
llist1 = reverse(llist.head)
print_doubly_linked_list(llist1, ' ', fptr)
fptr.write('\n')
fptr.close()
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[Programmers] 2019 KAKAO BLIND RECRUITMENT 실패율 (Python) (0) | 2021.02.12 |
---|---|
[Programmers] 2021 KAKAO BLIND RECRUITMENT신규 아이디 추천 (Python) (0) | 2021.02.11 |
[HackerRank] Insert a node at a specific position in a linked list (Python) (0) | 2021.02.08 |
[Programmers] 가운데 글자 가져오기 (Python) (0) | 2021.02.07 |
[Programmers] 나누어 떨어지는 숫자 배열 (Python) (0) | 2021.02.06 |