zoukankan      html  css  js  c++  java
  • 力扣(LeetCode)-两两交换链表中的结点

    解题思路

    先赞后看,时薪百万!

    首先我们用考虑把大象装进冰箱分几步法考虑这个题。

    我们要做的就是找到4个点:

    头(可以是空)
    点1
    点2
    尾(可以是空)

    当前:头(可以是空)-> 点1 -> 点2 -> 尾(可以是空)
    目标:头(可以是空)-> 点2 -> 点1 -> 尾(可以是空)

    我们需要干的活儿:
    步1. 让头指向点2
    步2. 让点2指向点1
    步3. 让点1指向尾

    然后就是,最开始其实是空的。所以先搞一个假头装上。
    最后返回的时候再从假头后面开始返回链表就ok啦。

    class Solution:
        def swapPairs(self, head: ListNode) -> ListNode:
    
            h = ListNode(0)
            h.next = head  # 假头
    
            temph = h  # 指针指向当前的头
    
            while temph.next and temph.next.next:  # 存在点1和点2
                node1 = temph.next
                node2 = temph.next.next
    
                temph.next = node2   # 步1
                node1.next = node2.next  # 步2
                node2.next = node1  # 步3
    
                temph = node1  # 移动指针,指向新的头
    
            return h.next
    

      

  • 相关阅读:
    linux 学习随笔-shell基础知识
    linux 学习随笔-压缩和解压缩
    解析xml的4种方法详解
    集合工具类
    Map概述
    List集合概述
    Java集合框架
    Spring JdbcTemplate详解
    关于c3p0数据库连接池的简单使用
    Java通过JDBC封装通用DAO层
  • 原文地址:https://www.cnblogs.com/lhy-522/p/13896431.html
Copyright © 2011-2022 走看看