Old Wei wins the offer to take you to learn --- Brush title series (15 reverse list)

15. Reverse list

problem:

After entering a list inverted list, the new list of the output header.

solve:

thought:

This question is typical routine to reverse a linked list, with a typical direct reversal of the list
Here Insert Picture Description

python code:

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        p=pHead
        q=None
        while(p!=None):
            r=q
            q=p
            p=p.next
            q.next=r
        return q
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/104903470