zoukankan      html  css  js  c++  java
  • 力扣(LeetCode)--206反转链表

    反转一个单链表。

    示例:

    输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL
    解题思路:

     

    class Solution(object):
    	def reverseList(self, head):
    		"""
    		:type head: ListNode
    		:rtype: ListNode
    		"""
    		# 申请两个节点,pre和 cur,pre指向None
    		pre = None
    		cur = head
    		# 遍历链表,while循环里面的内容其实可以写成一行
    		# 这里只做演示,就不搞那么骚气的写法了
    		while cur:
    			# 记录当前节点的下一个节点
    			tmp = cur.next
    			# 然后将当前节点指向pre
    			cur.next = pre
    			# pre和cur节点都前进一位
    			pre = cur
    			cur = tmp
    		return pre	
    

      

  • 相关阅读:
    11111
    单例-Singleton-03
    单例-Singleton-02
    单例-Singleton-01
    load和initialize
    OC中的static-01
    GCD-06
    GCD-05
    GCD-03
    UIView-01
  • 原文地址:https://www.cnblogs.com/lhy-522/p/13895193.html
Copyright © 2011-2022 走看看