反转链表
- 有递归和迭代两种方式
- 原理: 用next指针储存当前节点的next节点,用pre指针储存当前节点的前一个节点,每次遍历时,使得当前节点的next指向pre,并根据next指针变量,使当前节点前进,pre节点更新到当前节点位置。
两链表相交问题
- 问题一:判断一个链表是否有环,如果有,返回第一个相交节点
//获取环开始节点
public static Node getLoopNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node n1 = head.next;
Node n2 = head.next.next;
while (n1 != n2) {
if (n1.next == null || n2.next.next == null) {
return null;//无环情况
}
n1 = n1.next;
n2 = n2.next.next;
}
//while循环后,n2多n1一圈,此时n2回到起点,和n1的交点即为入圈点
n2 = head;
while (n1 != n2) {
n1 = n1.next;
n2 = n2.next;
}
return n1;
}
- 问题二:如何判断两无环链表是否相交,若相交,返回第一个相交点
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) {
cur1 = cur1.next;
n++;
}
while (cur2.next != null) {
cur2 = cur2.next;
n--;
}
//n>0 第一个链表更长,否则第二个链表更长,根据n值决定指针指向,并让长链表的指针
//先走abs(n)步,然后两指针同时走,第一个相遇点即为相交点
if (cur1 != cur2) {
return null;//不相交情况
}
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;
}
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
//loop1和loop2分别保存两链表入环的节点
Node cur1 = null;
Node cur2 = null;
if (loop1 == loop2) {
//如果loop1和loop2在同一结点,则从两链表头结点到loop结点搜索第一次相遇的结点,过程与无环链表情况相似
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
cur1 = cur1.next;
n++;
}
while (cur2 != loop2) {
cur2 = cur2.next;
n--;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n > 0) {
cur1 = cur1.next;
n--;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
} else {
//若loop结点不相同,则让loop1走一圈,若没有找到Loop2结点,则说明两链表不相交
cur1 = loop1.next;
while (cur1 != loop1) {
if (cur1 == loop2) {
return cur1;
}
cur1 = cur1.next;
}
return null;
}
}