1. 程式人生 > >劍指offer——反轉鏈表

劍指offer——反轉鏈表

劍指offer pytho sel 反轉鏈表 描述 鏈表 clas margin utf-8

題目描述

輸入一個鏈表,反轉鏈表後,輸出新鏈表的表頭。
# -*- 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
        if pHead==None:
            return None
        a=pHead
        b=pHead.next
        a.next=None
        while b :
            c=b.next
            b.next=a
            a=b
            b=c
        return a
            

劍指offer——反轉鏈表