算法体系-10 第十节:排序总结、链表相关面试题

一 算法的稳定性

1.1 概念 同样的值拍完序后相对次序不改变叫稳定性

1.1.1 对于基础类型的(int string )数组是没用的,学生(班级号 年龄 )先按年龄排序 再按班级号排序 如果年龄是一样的,如果按班级号排序是稳定排序,那么年龄相同的次序会传递下来

1.2 不同排序稳定性比较

选择排序不具备稳定性

基于比较

时间复杂度 额外空 间复杂度 稳定性 选择排序 O(N^2) O(1) 无 冒泡排序 O(N^2) O(1) 有 插入排序 O(N^2) O(1) 有 归并排序 O(NlogN) O(N) 有 随机快排 O(NlogN) O(logN) 无 堆排序 O(N*logN) O(1) 无

不基于比较

计数排序 O(N) O(M) 有

基数排序 O(N) O(N) 有

1.3 排序算法总结

1)不基于比较的排序,对样本数据有严格要求,不易改写

2)基于比较的排序,只要规定好两个样本怎么比大小就可以直接复用

3)基于比较的排序,时间复杂度的极限是O(NlogN)4)时间复杂度O(NlogN)、额外空间复杂度低于O(N)、且稳定的基于比较的排序是不存在的。

5)为了绝对的速度选快排(这三个当中快排的常数时间复杂度是最快的,大量实验得到的结果)、为了省空间选堆排、(因为数组就可以做堆,几个数就可以做完堆排序)为了稳定性选归并(合并的过程中相同的时候不合并)

1.4 java 里面的Array 排序是事实

工程上对排序的改进

Array内部基础类型使用的是快排,非基础类型使用归并排序因为基础类型更本不需要稳定性,谁排在那面都一样,如果是非基础类型的排序,他得要保证稳定性,选用归并排序

二 查找链表的中点

2.0 使用容器的方法

使用容器的方法

使用快慢指针查找中点

2.1 代码

1)输入链表头节点,奇数长度返回中点,偶数长度返回上中点前一个

2)输入链表头节点,奇数长度返回中点,偶数长度返回下中点

基数(中点) 偶数(上 下 )

3)输入链表头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个

4)输入链表头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个

基数(上中点) 偶数(上 下)

package class09;

import java.util.ArrayList;

public class Code01_LinkedListMid {

   public static class Node {
      public int value;
      public Node next;

      public Node(int v) {
         value = v;
      }
   }
   //总结 以基数返回他的中点为标准,找到 slow和fast的位置Node slow = head.next; Node fast = head.next.next;
  //1)输入链表头节点,奇数长度返回中点,偶数长度返回上中点
   // head 头
   public static Node midOrUpMidNode(Node head) {
      if (head == null || head.next == null || head.next.next == null) {
         return head;
      }
      // 链表有3个点或以上 下面的是三种情况在这题的基础上进行修改
      Node slow = head.next;
      Node fast = head.next.next;
      //上面的写法的等价于
       Node slow = head;
       Node fast = head;
      while (fast.next != null && fast.next.next != null) {
         slow = slow.next;
         fast = fast.next.next;
      }
      return slow;
   }
//2)输入链表头节点,奇数长度返回中点,偶数长度返回下中点
   public static Node midOrDownMidNode(Node head) {
      if (head == null || head.next == null) {
         return head;
      }
      Node slow = head.next;
      Node fast = head.next;
       // 因为是以fast他为终止条件的,他慢一步就是下一终点
      //上面的写法的等价于
      // Node slow = head; 让他快走一步 Node slow = head.next;
       //Node fast = head; 让他慢走一步Node fast = head.next;
      while (fast.next != null && fast.next.next != null) {
         slow = slow.next;
         fast = fast.next.next;
      }
      return slow;
   }
//3)输入链表头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个
   public static Node midOrUpMidPreNode(Node head) {
      if (head == null || head.next == null || head.next.next == null) {
         return null;
      }
      Node slow = head;
      Node fast = head.next.next;
      while (fast.next != null && fast.next.next != null) {
         slow = slow.next;
         fast = fast.next.next;
      }
      return slow;
   }
4)输入链表头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个
   public static Node midOrDownMidPreNode(Node head) {
      if (head == null || head.next == null) {
         return null;
      }
      if (head.next.next == null) {
         return head;
      }
      Node slow = head;
      Node fast = head.next;
      while (fast.next != null && fast.next.next != null) {
         slow = slow.next;
         fast = fast.next.next;
      }
      return slow;
   }

   public static Node right1(Node head) {
      if (head == null) {
         return null;
      }
      Node cur = head;
      ArrayList<Node> arr = new ArrayList<>();
      while (cur != null) {
         arr.add(cur);
         cur = cur.next;
      }
      return arr.get((arr.size() - 1) / 2);
   }

   public static Node right2(Node head) {
      if (head == null) {
         return null;
      }
      Node cur = head;
      ArrayList<Node> arr = new ArrayList<>();
      while (cur != null) {
         arr.add(cur);
         cur = cur.next;
      }
      return arr.get(arr.size() / 2);
   }

   public static Node right3(Node head) {
      if (head == null || head.next == null || head.next.next == null) {
         return null;
      }
      Node cur = head;
      ArrayList<Node> arr = new ArrayList<>();
      while (cur != null) {
         arr.add(cur);
         cur = cur.next;
      }
      return arr.get((arr.size() - 3) / 2);
   }

   public static Node right4(Node head) {
      if (head == null || head.next == null) {
         return null;
      }
      Node cur = head;
      ArrayList<Node> arr = new ArrayList<>();
      while (cur != null) {
         arr.add(cur);
         cur = cur.next;
      }
      return arr.get((arr.size() - 2) / 2);
   }

   public static void main(String[] args) {
      Node test = null;
      test = new Node(0);
      test.next = new Node(1);
      test.next.next = new Node(2);
      test.next.next.next = new Node(3);
      test.next.next.next.next = new Node(4);
      test.next.next.next.next.next = new Node(5);
      test.next.next.next.next.next.next = new Node(6);
      test.next.next.next.next.next.next.next = new Node(7);
      test.next.next.next.next.next.next.next.next = new Node(8);

      Node ans1 = null;
      Node ans2 = null;

      ans1 = midOrUpMidNode(test);
      ans2 = right1(test);
      System.out.println(ans1 != null ? ans1.value : "无");
      System.out.println(ans2 != null ? ans2.value : "无");

      ans1 = midOrDownMidNode(test);
      ans2 = right2(test);
      System.out.println(ans1 != null ? ans1.value : "无");
      System.out.println(ans2 != null ? ans2.value : "无");

      ans1 = midOrUpMidPreNode(test);
      ans2 = right3(test);
      System.out.println(ans1 != null ? ans1.value : "无");
      System.out.println(ans2 != null ? ans2.value : "无");

      ans1 = midOrDownMidPreNode(test);
      ans2 = right4(test);
      System.out.println(ans1 != null ? ans1.value : "无");
      System.out.println(ans2 != null ? ans2.value : "无");
   }

}

三 快慢指针相关面试题

3.1 面试时链表解题的方法论

1)对于笔试,不用太在乎空间复杂度,一切为了时间复杂度

2)对于面试,时间复杂度依然放在第一位,但是一定要找到空间最省的方法

四 判断该链表是否为回文结构 给定一个单链表的头节点head,请判断该链表是否为回文结构。

1)哈希表方法特别简单(笔试用)

用一个栈先将链表的数据放入栈中,再次遍历链表同时倒出栈里面的数据对比是否相等,因为栈里面的数据是先进后出的,遍历链表从前往后,出栈的是从后往前,如果说都相等的话就是回文结构

4.2 代码

public class Code02_IsPalindromeList {

   public static class Node {
      public int value;
      public Node next;

      public Node(int data) {
         this.value = data;
      }
   }

   // need n extra space
   public static boolean isPalindrome1(Node head) {
      Stack<Node> stack = new Stack<Node>();
      Node cur = head;
      while (cur != null) {
         stack.push(cur);
         cur = cur.next;
      }
      while (head != null) {
         if (head.value != stack.pop().value) {
            return false;
         }
         head = head.next;
      }
      return true;
   }

// need n/2 extra space
public static boolean isPalindrome2(Node head) {
   if (head == null || head.next == null) {
      return true;
   }
   Node right = head.next;
   Node cur = head;
   while (cur.next != null && cur.next.next != null) {
      right = right.next;
      cur = cur.next.next;
   }
   Stack<Node> stack = new Stack<Node>();
   while (right != null) {
      stack.push(right);
      right = right.next;
   }
   while (!stack.isEmpty()) {
      if (head.value != stack.pop().value) {
         return false;
      }
      head = head.next;
   }
   return true;
}

4.3)该原链表的方法就需要注意边界(面试用)

4.4 代码

// need O(1) extra space
public static boolean isPalindrome3(Node head) {
   if (head == null || head.next == null) {
      return true;
   }
   //常规的用这种就行,基数返回中点,偶数返回中点的上一个
   Node n1 = head.next;
   Node n2 = head.next.next;

   while (n2.next != null && n2.next.next != null) { // find mid node
      n1 = n1.next; // n1 -> mid
      n2 = n2.next.next; // n2 -> end
   }
   // n1 中点
   
   
   n2 = n1.next; // n2 -> right part first node
   n1.next = null; // mid.next -> null
   Node n3 = null;
   while (n2 != null) { // right part convert
      n3 = n2.next; // n3 -> save next node
      n2.next = n1; // next of right node convert
      n1 = n2; // n1 move
      n2 = n3; // n2 move
   }
   n3 = n1; // n3 -> save last node
   n2 = head;// n2 -> left first node
   boolean res = true;
   while (n1 != null && n2 != null) { // check palindrome
      if (n1.value != n2.value) {
         res = false;
         break;
      }
      n1 = n1.next; // left to mid
      n2 = n2.next; // right to mid
   }
   n1 = n3.next;
   n3.next = null;
   while (n1 != null) { // recover list
      n2 = n1.next;
      n1.next = n3;
      n3 = n1;
      n1 = n2;
   }
   return res;
}

五 将一个链表改为第一个和最后一个穿插连接的如下形式

方案:先将链表进行中间分开将后半进行反转,在进行遍历将前面一截的和后边一截的每一个挨着进行串联起来

六 单向链表按某值划分成左边小、中间相等、右边大的形式

1)把链表放入数组里,在数组上做partition(笔试用)类似快排

1.1 代码

public static class Node {
   public int value;
   public Node next;

   public Node(int data) {
      this.value = data;
   }
}

public static Node listPartition1(Node head, int pivot) {
   if (head == null) {
      return head;
   }
   Node cur = head;
   int i = 0;
   while (cur != null) {
      i++;
      cur = cur.next;
   }
   Node[] nodeArr = new Node[i];
   i = 0;
   cur = head;
   for (i = 0; i != nodeArr.length; i++) {
      nodeArr[i] = cur;
      cur = cur.next;
   }
   arrPartition(nodeArr, pivot);
   for (i = 1; i != nodeArr.length; i++) {
      nodeArr[i - 1].next = nodeArr[i];
   }
   nodeArr[i - 1].next = null;
   return nodeArr[0];
}

public static void arrPartition(Node[] nodeArr, int pivot) {
   int small = -1;
   int big = nodeArr.length;
   int index = 0;
   while (index != big) {
      if (nodeArr[index].value < pivot) {
         swap(nodeArr, ++small, index++);
      } else if (nodeArr[index].value == pivot) {
         index++;
      } else {
         swap(nodeArr, --big, index);
      }
   }
}

public static void swap(Node[] nodeArr, int a, int b) {
   Node tmp = nodeArr[a];
   nodeArr[a] = nodeArr[b];
   nodeArr[b] = tmp;
}

2)分成小、中、大三部分,再把各个部分之间串起来(面试用)

分析 准备如下形式的六个变量,线判断是否为空,为空的话遍历到满足该条件的节点头尾都指向他,下一个满足条件的就进行往下指

最后再将小尾连等头,等尾连大头

2.1 代码

public static class Node {
   public int value;
   public Node next;

   public Node(int data) {
      this.value = data;
   }
}

public static Node listPartition2(Node head, int pivot) {
   Node sH = null; // small head
   Node sT = null; // small tail
   Node eH = null; // equal head
   Node eT = null; // equal tail
   Node mH = null; // big head
   Node mT = null; // big tail
   Node next = null; // save next node
   // every node distributed to three lists
   while (head != null) {
      next = head.next;
      head.next = null;
      if (head.value < pivot) {
         if (sH == null) {
            sH = head;
            sT = head;
         } else {
            sT.next = head;
            sT = head;
         }
      } else if (head.value == pivot) {
         if (eH == null) {
            eH = head;
            eT = head;
         } else {
            eT.next = head;
            eT = head;
         }
      } else {
         if (mH == null) {
            mH = head;
            mT = head;
         } else {
            mT.next = head;
            mT = head;
         }
      }
      head = next;
   }
   // 小于区域的尾巴,连等于区域的头,等于区域的尾巴连大于区域的头
   if (sT != null) { // 如果有小于区域
      sT.next = eH;
      eT = eT == null ? sT : eT; // 下一步,谁去连大于区域的头,谁就变成eT
   }
   // 下一步,一定是需要用eT 去接 大于区域的头
   // 有等于区域,eT -> 等于区域的尾结点
   // 无等于区域,eT -> 小于区域的尾结点
   // eT 尽量不为空的尾巴节点
   if (eT != null) { // 如果小于区域和等于区域,不是都没有
      eT.next = mH;
   }
   return sH != null ? sH : (eH != null ? eH : mH);
}

public static void printLinkedList(Node node) {
   System.out.print("Linked List: ");
   while (node != null) {
      System.out.print(node.value + " ");
      node = node.next;
   }
   System.out.println();
}

七 一种特殊的单链表节点类描述如下 (暂时不学)

一种特殊的单链表节点类描述如下

class Node {

int value;

Node next;

Node rand;

Node(int val) {

value = val;

}

}

rand指针是单链表节点结构中新增的指针,rand可能指向链表中的任意一个节点,也可能指向null。

给定一个由Node节点类型组成的无环单链表的头节点 head,请实现一个函数完成这个链表的复制,新拷贝出来的链表节点和指针指向和原链表一样, 并返回复制的新链表的头节点。

【要求】

时间复杂度O(N),额外空间复杂度O(1)

7.1 代码

public class Code04_CopyListWithRandom {

   public static class Node {
      int val;
      Node next;
      Node random;

      public Node(int val) {
         this.val = val;
         this.next = null;
         this.random = null;
      }
   }

   public static Node copyRandomList1(Node head) {
      // key 老节点
      // value 新节点
      HashMap<Node, Node> map = new HashMap<Node, Node>();
      Node cur = head;
      while (cur != null) {
         map.put(cur, new Node(cur.val));
         cur = cur.next;
      }
      cur = head;
      while (cur != null) {
         // cur 老
         // map.get(cur) 新
         // 新.next ->  cur.next克隆节点找到
         map.get(cur).next = map.get(cur.next);
         map.get(cur).random = map.get(cur.random);
         cur = cur.next;
      }
      return map.get(head);
   }

   public static Node copyRandomList2(Node head) {
      if (head == null) {
         return null;
      }
      Node cur = head;
      Node next = null;
      // 1 -> 2 -> 3 -> null
      // 1 -> 1' -> 2 -> 2' -> 3 -> 3'
      while (cur != null) {
         next = cur.next;
         cur.next = new Node(cur.val);
         cur.next.next = next;
         cur = next;
      }
      cur = head;
      Node copy = null;
      // 1 1' 2 2' 3 3'
      // 依次设置 1' 2' 3' random指针
      while (cur != null) {
         next = cur.next.next;
         copy = cur.next;
         copy.random = cur.random != null ? cur.random.next : null;
         cur = next;
      }
      Node res = head.next;
      cur = head;
      // 老 新 混在一起,next方向上,random正确
      // next方向上,把新老链表分离
      while (cur != null) {
         next = cur.next.next;
         copy = cur.next;
         cur.next = next;
         copy.next = next != null ? next.next : null;
         cur = next;
      }
      return res;
   }

}

  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值