zoukankan      html  css  js  c++  java
  • 剑指 Offer 24. 反转链表

    剑指 Offer 24. 反转链表

    地址:剑指 Offer 24. 反转链表

    问题描述:

    定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

    示例:

    输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL

    限制:

    0 <= 节点个数 <= 5000

    /**
     * Definition for singly-linked list.
     * class ListNode(var _x: Int = 0) {
     *   var next: ListNode = null
     *   var x: Int = _x
     * }
     */
    object Solution {
        def reverseList(head: ListNode): ListNode = {
            if (head == null) return head
    
            val dummyHead = ListNode(0)
            dummyHead.next = head
            var newHead = head
            var tail = head
            while (newHead.next != null) {
                val temp = newHead.next
                newHead.next = temp.next
                dummyHead.next = temp
                temp.next = tail
                tail = temp
            }
            return dummyHead.next
        }
    }
    
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func reverseList(head *ListNode) *ListNode {
        if head == nil {return head}
        dummyHead := &ListNode{Val: 0 , Next: head}
        tail := head
        for head.Next != nil {
            temp := head.Next
            head.Next = temp.Next
            dummyHead.Next = temp
            temp.Next = tail
            tail = temp  
        }
        return dummyHead.Next
    }
    
  • 相关阅读:
    JDBC数据库连接
    JDBC驱动程序类型
    JDBC简单示例代码
    JDBC SQL语法
    JDBC是什么?
    JDBC快速入门教程
    JUnit4参数化测试实例
    JUnit套件测试实例
    JUnit4时间(超时)测试实例
    JUnit4忽略(Ignore)测试实例
  • 原文地址:https://www.cnblogs.com/ganshuoos/p/14190113.html
Copyright © 2011-2022 走看看