1, 给定一个单链表,检查是否有环:定义两个指针fast,slow,步长分别为2和1,从头指针开始,如果相遇了,就说明存在环,否则不存在
1)如何知道环的长度:从第一次碰撞点开始,fast,slow指针开始走,再次相遇所走过的操作数就是环的长度,即环中元素的个数。
2)如何知道环的连接点:分别从头指针和相遇点开始单步往下走,再次相遇点就是连接点;还有一种方法利用链表是否相交来获取。
3)如何知道此链表元素的个数:利用1)和2)的结果,可以得到
2, 给定两个单链表判断是否相交
4,给定单链表中某个节点p(不是最后一个节点),删除该节点,没有头节点的情况下。
5,给定单链表中某个节点p(非空节点),在该节点前插入一个节点q。
6, 反转一个单链表
Java代码:
定义节点:
class Node {
// 为了简化,将成员变量public出来,不用使用get/set来获得
public String data;
public Node next;
Node(String data, Node next) {
this.data = data;
this.next = next;
}
}
1, 给定一个单链表,检查是否有环:
/*
* 判断链表是否存在环
*/
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
if(fast==slow){
return true;
}
}
return false;
}
1.1 如何知道环的长度:
/**
* 得到环中元素的个数,从相遇点开始,fast和slow再次移动,如果相遇,走过的操作数就是环中
* 元素的个数
* @param head
* @return
*/
public static int getCircleLength(Node head){
int length = 0;
Node meetNode = checkExistCyle(head);
if(meetNode==null){
return length;
}
Node slow = meetNode;
Node fast = meetNode;
// slow每次前进一步,fast每次前进两步,如果slow和fast指向同一个节点,就存在环
while (true) {
slow = slow.next;
fast = fast.next.next;
length ++;
if (slow == fast) {
return length;
}
}
}
1.2 如何知道环的连接点:
方法一:
/**
* 得到带环链表的接入点,分别从头指针和相遇点开始单步往下走,再次相遇点就是连接点
* @param head
* @return
*/
public static Node getCrossedNode(Node head){
Node meetNode = checkExistCyle(head);
if(meetNode==null)
return null;
Node p1 = head;
Node p2 = meetNode;
while(true){
if(p1==p2)
return p1;
p1 = p1.next;
p2 = p2.next;
}
}
方法二:
/**
* 得到带环链表的接入节点,利用两条链表是否相交得到
* @param head
* @return
*/
public static Node getCrossedNode2(Node head){
Node crossedNode = checkExistCyle(head);
if(crossedNode!=null){
Node p2 = crossedNode.next;
crossedNode.next = null;
return checkIsCrossedList(head, p2);
}
return null;
}
1.3 如何知道此链表元素的个数:环中的个数+头节点到接入点的个数:
/**
* 得到环中元素的个数,从相遇点开始,fast和slow再次移动,如果相遇,走过的操作数就是环中
* 元素的个数
* @param head
* @return
*/
public static int getCircleLength(Node head){
int length = 0;
Node meetNode = checkExistCyle(head);
if(meetNode==null){
return length;
}
Node slow = meetNode;
Node fast = meetNode;
// slow每次前进一步,fast每次前进两步,如果slow和fast指向同一个节点,就存在环
while (true) {
slow = slow.next;
fast = fast.next.next;
length ++;
if (slow == fast) {
return length;
}
}
}
/**
* 得到从头指针到相交点的元素个数,如果不存在环,返回0
* @param head
* @return
*/
public static int getLengthFromHeadToCrossed(Node head){
int length = 0;
Node p1 = head;
Node meetNode = checkExistCyle(head);
//不存在环,遍历链表得到长度
if(meetNode==null){
return length;
}
p1 = head;
Node p2 = meetNode;
while(true){
length++;
if(p1==p2)
return length;
p1 = p1.next;
p2 = p2.next;
}
}
2, 给定两个单链表判断是否相交
/**
* 判断两个链表是否存在相交的节点,存在的话,返回此节点
* @param head1
* @param head2
* @return
*/
public static Node checkIsCrossedList(Node head1, Node head2) {
int len1 = 0;
int len2 = 0;
int len = 0;
Node p1 = head1, p2 = head2;
while (p1 != null) {
len1++;
p1 = p1.next;
}
while (p2 != null) {
len2++;
p2 = p2.next;
}
if (len1 >= len2) {
len = len1 - len2;
p1 = head1;
p2 = head2;
// p1移到head1+len处
while (len > 0) {
p1 = p1.next;
len--;
}
// p1,p2等步长往后移动,如果p1==p2则相交,否则不相交
while (p1 != null && p2 != null && p1 != p2) {
p1 = p1.next;
p2 = p2.next;
}
if (p1 != null && p2 != null && p1 == p2) {
return p1;
}
} else {
len = len2 - len1;
p1 = head1;
p2 = head2;
// p2移到head2+len处
while (len > 0) {
p2 = p2.next;
len--;
}
// p1,p2等步长往后移动,如果p1==p2则相交,否则不相交
while (p1 != null && p2 != null && p1 != p2) {
p1 = p1.next;
p2 = p2.next;
}
if (p1 != null && p2 != null && p1 == p2) {
return p1;
}
}
return null;
}
6, 反转一个单链表:遍历法和递归法:
/*
* 将一个单链表倒转
*/
public static Node reverse(Node head) {
Node pre = null;
Node cur = head;
Node next = null;
if (head == null || head.next == null) {
return head;
}
while (cur != null) {
// 先记录下剩下链表的头
next = cur.next;
// 将当前节点指向前一个节点,实现反转
cur.next = pre;
// 当前节点变成了前一个节点
pre = cur;
// 下一个节点变成了当前节点
cur = next;
}
head = pre;
return head;
}
/*
* 递归方法实现倒转单链表,倒转当前节点前,先倒转后续节点,即假设后续节点为头的链表已经反转完毕。
* 当递推到最后两个节点时,就不再往下递推了,将最后两个节点反转 然后向上回溯
*/
public static Node reverseByRecurve(Node head) {
if (head == null || head.next == null) {
return head;
}
Node reversedHead = reverseByRecurve(head.next);
head.next.next = head;
head.next = null;
return reversedHead;
}