zoukankan      html  css  js  c++  java
  • [算法]链表题目

    两两交换链表中的节点

    题目描述

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

    你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

    示例:

    给定 1->2->3->4, 你应该返回 2->1->4->3.

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路

    先提供一个投机取巧的办法,既然转换链表中的前后关系比较麻烦,那我不妨反其道而行之,我直接交换链表的值,让链表的关系保持不变即可。

    还有一种思路是通过递归来计算。

    代码

    解法1:

    /**
     * Definition for singly-linked list.
     * class ListNode(var _x: Int = 0) {
     *   var next: ListNode = null
     *   var x: Int = _x
     * }
     */
    object Solution {
        def swapPairs(head: ListNode): ListNode = {
            var newHead = head
            while(newHead != null && newHead.next != null){
                val temp = newHead.x
                newHead.x = newHead.next.x
                newHead.next.x = temp
                newHead = newHead.next.next
            }
            head
        }
    }

    解法2:

    class Solution {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
            ListNode next = head.next;
            head.next = swapPairs(next.next);
            next.next = head;
            return next;
        }
    }

    反转链表II

    题目描述

    反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。

    说明:
    1 ≤ m ≤ n ≤ 链表长度。

    示例:

    输入: 1->2->3->4->5->NULL, m = 2, n = 4
    输出: 1->4->3->2->5->NULL

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/reverse-linked-list-ii
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路

    同上,投机取巧的办法只需要交换值即可,用一个栈来临时存储数据。

    也可以参考之前反转链表的思路,将链表分成三段,m之前的,m到n的,n之后的。

    代码

    解法1(扫描了2次,不太符合要求):

    /**
     * Definition for singly-linked list.
     * class ListNode(var _x: Int = 0) {
     *   var next: ListNode = null
     *   var x: Int = _x
     * }
     */
    import java.util.Stack
    object Solution {
        def reverseBetween(head: ListNode, m: Int, n: Int): ListNode = {
            var newHead = head
            var index = 1
            val stack: Stack[Int] = new Stack[Int]()
            while(newHead != null){
              if(index >= m && index <= n){
                stack.push(newHead.x)
              }
              newHead = newHead.next
              index += 1
            }
    
            newHead = head
            index = 1
            while(newHead != null){
              if(index >= m && index <= n){
                newHead.x = stack.pop()
              }
              newHead = newHead.next
              index += 1
            }
            head
        }
    }

    解法2:

    class Solution {
        public ListNode reverseBetween(ListNode head, int m, int n) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode cur = head, pre = dummy;
            int i = 1;
            while (i < m) {//通过i去寻找开始反转的地方
                pre = cur;
                cur = cur.next;
                i++;
            }
            ListNode node = pre;//node记录的是反转链表的前一个节点
    
            //反转从m到n的链表,解法同206题
            while (i <= n) {
                ListNode temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
                i++;
            }
    
            //反转链表接回原链表
            node.next.next = cur;
            node.next = pre;
            return dummy.next;
    
        }
    }

    解释下子链表怎么接回原链表:

    1->2->3->4->5->null, m = 2, n = 4,

    子链表反转完成后是这样的

    1<->2<-3<-4 5->null,此时cur = 5, pre = 4, node = 1,接下来就显而易见了。

    https://leetcode-cn.com/problems/reverse-linked-list-ii/solution/fan-zhuan-lian-biao-ii-by-leetcode/

  • 相关阅读:
    远程网络时间同步在分布式测控与实时仿真系统应用
    GPS对时装置(北斗卫星同步时钟)应用市场调研分析
    时间同步服务器(NTP时钟同步服务器)如何正确的选购?
    NTP授时服务器(卫星同步时钟)与物联网十大应用
    App 自动化环境搭建(基于 Appium)
    let var作用域
    vue methods和computed效率比较
    vue computed
    vue computed
    vue v-bind:style
  • 原文地址:https://www.cnblogs.com/DarrenChan/p/11336162.html
Copyright © 2011-2022 走看看