zoukankan      html  css  js  c++  java
  • 24. Swap Nodes in Pairs

    package LeetCode_24
    
    /**
     * 24. Swap Nodes in Pairs
     * https://leetcode.com/problems/swap-nodes-in-pairs/description/
     *
     * Given a linked list, swap every two adjacent nodes and return its head.
    You may not modify the values in the list's nodes, only nodes itself may be changed.
    
    Example:
    Given 1->2->3->4, you should return the list as 2->1->4->3.
     * */
    class ListNode(var `val`: Int) {
        var next: ListNode? = null
    }
    
    class Solution {
        /*
        * Time complexity:O(n), Space complexity:O(1)
        * */
        fun swapPairs(head: ListNode?): ListNode? {
            if (head == null || head.next == null) {
                return head
            }
            val dummy = ListNode(-1)
            var prve = dummy
            var first: ListNode? = null
            dummy.next = head
            while (prve != null) {
                if (prve.next == null || prve.next!!.next == null) {
                    break
                }
                first = prve.next
                prve.next = first!!.next
                first.next = first.next!!.next
                prve.next!!.next = first
                prve = first
            }
            return dummy.next
        }
    }

    参考此文章

  • 相关阅读:
    第九周作业
    第八周
    第七周
    Jmeter连接到Mysql
    数据库常用链接URL写法
    功能测试方法
    常建输入框的测试
    系统业务流程测试(转)
    Linux
    搭建Git服务器
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13631609.html
Copyright © 2011-2022 走看看