# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
cursor = head
result_list_prev_node = None
while cursor:
# print(cursor.val, new_list_prev_node)
next_node = cursor.next
cursor.next = result_list_prev_node
result_list_prev_node = cursor
if next_node:
cursor = next_node
else:
return cursor
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None:
return head
# 1. prev, current, next 초기화
prev = None
current = head
next = head.next
# 2. 링크드 리스트를 순회
while True:
if next is None:
# current node is Head
if current is None:
return current
current.next = prev
return current
# Reverse Node
current.next = prev
# Update prev, current, next
prev = current
current = next
next = current.next