zoukankan      html  css  js  c++  java
  • 1721. Swapping Nodes in a Linked List

    package LeetCode_1721
    
    /**
     * 1721. Swapping Nodes in a Linked List
     * https://leetcode.com/problems/swapping-nodes-in-a-linked-list/
     * You are given the head of a linked list, and an integer k.
    Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).
    
    Example 1:
    Input: head = [1,2,3,4,5], k = 2
    Output: [1,4,3,2,5]
    
    Example 2:
    Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
    Output: [7,9,6,6,8,7,3,0,9,5]
    
    Example 3:
    Input: head = [1], k = 1
    Output: [1]
    
    Example 4:
    Input: head = [1,2], k = 1
    Output: [2,1]
    
    Example 5:
    Input: head = [1,2,3], k = 2
    Output: [1,2,3]
    
    Constraints:
    1. The number of nodes in the list is n.
    2. 1 <= k <= n <= 105
    3. 0 <= Node.val <= 100
     * */
    class ListNode(var `val`: Int) {
        var next: ListNode? = null
    }
    
    class Solution {
        /*
        * solution 1: change Linked List to array, return result Linked List after swap array, Time:O(n), Space:O(n);
        * solution 2: find out k-th from start and from end, swap both, Time:O(n), Space:O(1);
        * */
        fun swapNodes(head: ListNode?, k: Int): ListNode? {
            //solution 1:
            var head_ = head
            var length = 0
            while (head_ != null) {
                length++
                head_ = head_.next
            }
            if (k == 1 && length == k) {
                return head
            }
            val reverseK = length - k
            val array = IntArray(length)
            head_ = head
            var i = 0
            while (head_ != null) {
                array[i++] = head_.`val`
                head_ = head_.next
            }
            swap(array, k - 1, reverseK)
            val dummy = ListNode(-1)
            var head2 = dummy
            for (item in array) {
                head2.next = ListNode(item)
                head2 = head2.next!!
            }
            return dummy.next
    
        }
    
        private fun swap(nums: IntArray, i: Int, j: Int) {
            val temp = nums[i]
            nums[i] = nums[j]
            nums[j] = temp
        }
    }

    solution 2

     fun swapNodes(head: ListNode?, k: Int): ListNode? {
            //solution 2
            var kNodeFromStart = head
            var kNodeFromEnd = head
            var current = head
            //find out k-th node from start
            for (i in 0 until k - 1) {
                current = current!!.next
            }
            kNodeFromStart = current
            //find out k-th node from end
            while (current!!.next != null) {
                /*
                * kNodeFromEnd is start from 0, so if current.next reach the end, kNodeFromEnd is at k-th start from end
                * */
                kNodeFromEnd = kNodeFromEnd!!.next
                current = current.next
            }
            //swap
            val temp = kNodeFromStart!!.`val`
            kNodeFromStart!!.`val` = kNodeFromEnd!!.`val`
            kNodeFromEnd!!.`val` = temp
            return head
        }
  • 相关阅读:
    Android OCR 之 tesseract
    抛砖引玉 之 谁动了我的流量(0权限上传数据)
    退伍一年了
    android 通过 Hessian 与 j2ee 服务端交互
    Arduino 入手
    抛砖引玉 之 谁动了我的隐私(android用户隐私窥探)
    如何关注那些有价值的微博
    关于培训和外包20111027
    ASP.NET实现Cookie功能的三个基本操作(写入,读取,删除)
    提高你开发效率的十五个Visual Studio 2010使用技巧
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14257931.html
Copyright © 2011-2022 走看看