LeetCode primary algorithm - list 01: reverse list
Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
CSDN: HTTPS: //blog.csdn. NET / abcgkj /
GitHub: https://github.com/aimi-cn/AILearners
First, the primer
It was launched by the LeetCode official list of interview questions classics -
this module is to explore the corresponding primary algorithm - designed to help entry-algorithm. Our first pass leetcode brush is recommended topics.
View the complete algorithm Offer to prove safety issues resolved, please click on the link github:
github address
Second, the title
Reverse a singly linked list.
Example:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
Advanced:
You can reverse the iterative or recursively list. Can you solve this question in two ways?
1, ideas
The problem for me was a little more difficult, in fact, the principle is not difficult, we use our three pointers, pointing to the current traverse to the node, a node before it and after a node.
When traversing, substitutions do tail current node and the previous node of a node.
Because in the brush LeetCode has been done before on this topic in detail illustrates the link you can see everyone: https://blog.csdn.net/baidu_31657889/article/details/91552141
2, programming
python
# 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 head==None or head.next==None:
return head
pre = None
while head:
next = head.next
head.next = pre
pre = head
head = next
return pre
AIMI-CN AI learning exchange group [1015286623] for more information on AI
Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!
This article from the blog article multiple platforms OpenWrite release!