本文主要是介绍[LeetCode] 206. Reverse Linked List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目内容
https://leetcode-cn.com/problems/reverse-linked-list/
Reverse a singly linked list.
Example:Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
题目思路
这个就是基本的尾插法构建链表。
程序代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution(object):def reverseList(self, head):""":type head: ListNode:rtype: ListNode"""if not head:return Nonenh=ListNode(0)#p=nhwhile head:tmp=ListNode(head.val)tmp.next=nh.nextnh.next=tmphead=head.nextreturn nh.next
这篇关于[LeetCode] 206. Reverse Linked List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!