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
        }
  • 相关阅读:
    8 Django 模型层(1)
    7 Django的模板层
    Java ClassLoader
    Spring的注入注解
    Java 面试Spring的加载机制
    Spring容器启动初始化bean的方法
    java 线程
    经典博客
    Spring注解@Component、@Repository、@Service、@Controller区别
    java 实现多个文件的Zip包的生成
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14257931.html
Copyright © 2011-2022 走看看