Topic link
https://leetcode.com/problems/reverse-linked-list/
The questions in the original
Reverse a singly linked list.
The title translation
Flip a one-way linked list
Thinking method
This topic is relatively basic, and there are many solutions to it, and there are also many solutions to AC. Here, only part of the ideas are sorted out for reference.
Thinking a
Using the stack structure, the contents of the linked list can be pushed onto the stack in turn, and then ejected from the stack in turn to construct the reverse order. The following code emulates the stack with ordinary arrays.
code
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
p = head
newList = []
while p:
newList.insert(0, p.val)
p = p.next
p = head
for v in newList:
p.val = v
p = p.next
return head
Idea 2
Similar to the idea of a stack, but directly in the original linked list operation, by iterating the node reorganization, the previous node moved to the back of the reorganized list. It’s actually an upside-down operation.
code
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
new_head = None
while head:
p = head
head = head.next
p.next = new_head
new_head = p
return new_head
Thought three
Recursion. Notice the termination condition
code
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next:
return head
p = head.next
n = self.reverseList(p)
head.next = None
p.next = head
return n
PS: The novice brush LeetCode, the new handwriting blog, write wrong or write unclear please help point out, thank you!
reprint please indicate the: http://blog.csdn.net/coder_orz/article/details/51306170
Read More:
- 【Hackerrank】Reverse a doubly linked list
- Assign the intersection combination of two linked lists to linked list a
- Leetcode-234: palindrome linked list
- 7. Reverse Integer [easy] (Python)
- 【Bug-python】IndexError: list index out of range
- Pre initialization of list content and length in Python
- JMeter linked database
- python-TypeError: list indices must be integers, not tuple Solution
- python3 Error in sitecustomize; set PYTHONVERBOSE for traceback: NameError: name ‘modules_list’ …
- Python + selenium drop-down list error message: element not interactive
- 21. Merge Two Sorted Lists [easy] (Python)
- Reverse function: reverses container contents
- Codeworks educational round 96 [reverse pair] E. string reverse
- Leetcode: 7. Reverse Integer(JAVA)
- How to Use the Reverse() Function
- Nginx reverse proxy MySQL
- IOS reverse error: use of undeclared identifier ‘mshookivar’
- Mybatis reverse problem failed to load ApplicationContext
- Nginx reverse proxy report 400 error solution!
- NxL job cluster nginx routing forwarding and reverse proxy