本文主要是介绍剑指offer15.反转链表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage=1&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
题目描述
输入一个链表,反转链表后,输出新链表的表头。
先用tmp储存head.next,然后将head插入res后面,最后将tmp赋值给head:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:# 返回ListNodedef ReverseList(self, pHead):# write code hereres = ListNode(0)while pHead:tmp = pHead.nextpHead.next = res.nextres.next = pHeadpHead = tmpreturn res.next
这篇关于剑指offer15.反转链表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!