Created
March 6, 2018 08:32
-
-
Save vimiix/869340fc3ce4c8b111d4e8877704fba7 to your computer and use it in GitHub Desktop.
翻转单链表
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 is None or pHead.next is None: | |
return pHead | |
pre = None | |
cur = pHead | |
new_head = cur | |
while cur: | |
new_head = cur | |
next = cur.next | |
cur.next = pre | |
pre = cur | |
cur = next | |
return new_head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment