几个链表相关的题目

94 阅读4分钟

回文链表判断

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

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

2)改原链表的方法就需要注意边界了(面试用)

// 利用栈先进后出的特性,很简单,但是空间复杂度高
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;
}

// 比较难写,但是空间复杂度低
public static boolean isPalindrome3(Node head) {
   if (head == null || head.next == null) {
      return true;
   }
   Node n1 = head;   // 慢指针,一次一步
   Node n2 = head; // 快指针,一次两步
   while (n2.next != null && n2.next.next != null) { // find mid node
      n1 = n1.next; // 奇数,正好中点;偶数,中点偏左位置
      n2 = n2.next.next; // n2位置不重要,主要是判断何时停止循环
   }
   n2 = n1.next; // n2 -> right part first node
   n1.next = null; // mid.next -> null
   Node n3 = null;
   while (n2 != null) { // 右侧链表翻转
      n3 = n2.next;  // n3相当于next
      n2.next = n1;  // n1相当于pre
      n1 = n2;
      n2 = n3;
   }
   n3 = n1; // n1此时来到了最后一个节点,用n3保存
   n2 = head;
   boolean res = true;
   while (n1 != null && n2 != null) { // 判断回文,中点next为null,此时停止
      if (n1.value != n2.value) {
         res = false;
         break;
      }
      n1 = n1.next; // left to mid
      n2 = n2.next; // right to mid
   }
   n1 = n3.next; // n3之前保存过,为最后的节点
   n3.next = null;
   while (n1 != null) { // 还原链表
      n2 = n1.next; // n1为cur,n2为next,n3为pre
      n1.next = n3;
      n3 = n1;
      n1 = n2;
   }
   return res;
}

复制带随机指针的链表

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; // 记录cur指针,下次应该到的位置
   // 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;
}

链表中,等于某个值的节点放中间,小于放左,大于放右、

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

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

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

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

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

// 按照netherlandsFlag实现即可
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;
}

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);
}

两个链表是否相交,相交则返回第一个相交节点

给定两个可能有环也可能无环的单链表,头节点head1和head2。请实现一个函数,如果两个链表相交,请返回相交的 第一个节点。如果不相交,返回null。如果两个链表长度之和为N,时间复杂度请达到O(N),额外空间复杂度 请达到O(1)。

public static Node getIntersectNode(Node head1, Node head2) {
   if (head1 == null || head2 == null) {
      return null;
   }
   Node loop1 = getLoopNode(head1);
   Node loop2 = getLoopNode(head2);
   // 不可能一个有环,一个没环
   if (loop1 == null && loop2 == null) { // 都没有环
      return noLoop(head1, head2);
   }
   if (loop1 != null && loop2 != null) { // 都有环
      return bothLoop(head1, loop1, head2, loop2);
   }
   return null;
}

// 找到链表第一个入环节点,如果无环,返回null
public static Node getLoopNode(Node head) {
   if (head == null || head.next == null || head.next.next == null) {
      return null;
   }
   // n1 慢  n2 快
   Node slow = head.next; // n1 -> slow
   Node fast = head.next.next; // n2 -> fast
   while (slow != fast) {
      if (fast.next == null || fast.next.next == null) {
         return null;
      }
      fast = fast.next.next;
      slow = slow.next;
   }
   // slow fast  相遇
   fast = head;
   while (slow != fast) { // 从头开始走,总能相遇
      slow = slow.next;
      fast = fast.next;
   }
   return slow;
}

// 如果两个链表都无环,返回第一个相交节点,如果不想交,返回null
public static Node noLoop(Node head1, Node head2) {
   if (head1 == null || head2 == null) {
      return null;
   }
   Node cur1 = head1;
   Node cur2 = head2;
   int n = 0;

   while (cur1.next != null) { // 走到最后一个节点,停
      n++;
      cur1 = cur1.next;
   }
   while (cur2.next != null) {
      n--;
      cur2 = cur2.next;
   }
   if (cur1 != cur2) { // 如果二者的尾节点不相同,显然不相交
      return null;
   }
   // n  :  链表1长度减去链表2长度的值
   cur1 = n > 0 ? head1 : head2; // 谁长,谁的头变成cur1
   cur2 = cur1 == head1 ? head2 : head1; // 谁短,谁的头变成cur2
   n = Math.abs(n);
   while (n != 0) { // 这个循环结束,二者和交点的距离相同
      n--;
      cur1 = cur1.next;
   }
   while (cur1 != cur2) {
      cur1 = cur1.next;
      cur2 = cur2.next;
   }
   return cur1;
}

// 两个有环链表,返回第一个相交节点,如果不想交返回null
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
   Node cur1 = null;
   Node cur2 = null;
   if (loop1 == loop2) { // 交点相同
      cur1 = head1;
      cur2 = head2;
      int n = 0;
      while (cur1 != loop1) {
         n++;
         cur1 = cur1.next;
      }
      while (cur2 != loop2) {
         n--;
         cur2 = cur2.next;
      }
      cur1 = n > 0 ? head1 : head2;
      cur2 = cur1 == head1 ? head2 : head1;
      n = Math.abs(n);
      while (n != 0) {
         n--;
         cur1 = cur1.next;
      }
      while (cur1 != cur2) {
         cur1 = cur1.next;
         cur2 = cur2.next;
      }
      return cur1;
   } else { // 相交,交点不同 或者 不相交
      cur1 = loop1.next;
      while (cur1 != loop1) { // 从其中一个交点出发,找不到第二个交点就是不相交
         if (cur1 == loop2) {
            return loop1;
         }
         cur1 = cur1.next;
      }
      return null;
   }
}