数据结构之链表及相关算法

链表的定义

链表是链式存储结构,可以用任意一组存储单元来存储单链表中的数据元素,存储单元可以是不连续的。除了存储每个数据元素的值之外,还必须存储知识其直接后继元素的信息。定义如下的数据类存储结点信息。

//链表结点的定义
class Node{
	Node next = null; // 结点域
	int data; // 数据域
	public Node(int data){
		this.data = data;
	}
}

链表的创建

给定一个数组,用链表来存储这个数组。循环遍历数组,把每个元素链接到链表中,最后返回头结点。

public class TestNode{
	
	// 输入一个整型数组,创建成链表
	public Node createLinkList(int[] arr){
		Node head = new Node(arr[0]); //链表头
		Node r = head; // 指向链表尾结点
		for(int i=1; i<arr.length; i++){
			Node pNode = new Node(arr[i]); // 新结点的产生
		    r.next = pNode; // 链表尾部增加新结点
		    r = pNode; // r更新为新加的结点
		}
		return head;
	}
	
	// 返回链表的长度
	public int length(Node head) {
		int length = 0;
		Node tmp = head;
		while(tmp != null){
			length++;
			tmp = tmp.next;
		}
		return length;
	}
	
	// 顺序打印链表元素信息
	public void printLinkList(Node head){
		Node p = head;
		while(p != null){
			System.out.print(p.data+"->");
			p = p.next;
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		int[] arr = {1,3,6,8,12,15,20};
		TestNode testNode = new TestNode();
		Node head = testNode.createLinkList(arr);
		testNode.printLinkList(head);
	}
}

链表的相关算法

1. 找出单链表中的倒数第k个元素

设置两个指针p1、p2,p1先前移k-1步,然后p1、p2同时向前移动。循环直到先行的p1值为NULL时,p2所指的位置就是所要找的倒数第k个位置。

//找出单链表中的倒数第k个元素
public Node findElem(Node head, int k){
	if(k < 1) return null;
	Node p1 = head;
	Node p2 = head;
	for(int i=0; i<k-1; i++){ // p1先走k-1步
		p1 = p1.next;
		if(p1 == null) return null;
	}
	while(p1 != null){ // p1,p2同时走,直到p1为null
		p1 = p1.next;
		p2 = p2.next;
	}
	return p2;
}

2. 实现链表的反转

反转一个链表,需要调整指针的指向,具体需要操作3个相邻的结点。

//如何实现链表的反转
public Node ReverseIteratively(Node head){
	Node pReverseHead = head;
	Node pNode = head;
	Node pPrev = null;
	while(pNode != null){
		Node pNext = pNode.next;
		if(pNext == null)
			pReverseHead = pNode;
		pNode.next = pPrev;
		pPrev = pNode;
		pNode = pNext;
	}
	return pReverseHead;
}

3. 寻找单链表的中间结点

定义两个指针p、q,从头开始遍历,p一次走两步,q一次走一步,p先到链表尾部,q则恰好到达链表中部。

// 寻找单链表的中间结点
public Node SearchMid(Node head){
	Node p = head; 
	Node q = head; // 定义两个指针
	while(p != null && p.next != null){
		p = p.next.next; // 一个每次走两步
		q = q.next; // 一个每次走一步
	} // 每次走两步的指针p到结尾时,每次走一步的指针q刚好到中间结点
	return q;
}

4. 合并两个有序链表,并使合并后的链表也有序

// 合并两个有序链表,并使合并后的链表也有序
public Node mergeLink(Node head1, Node head2){
	if(head1 == null) return head2; // 检测head1是否为空
	else if(head2 == null) return head1; // 检测head2是否为空
	
	Node head = null;
	Node p1 = head1;
	Node p2 = head2;
	if(head1.data < head2.data){
		head = head1;
		p1 = head1.next;
	}else{
		head = head2;
		p2 = head2.next;
	}
	Node pNode = head; 
	
	while(p1 != null && p2 != null){
		if(p1.data > p2.data){
			pNode.next = p2;
			p2 = p2.next;
		}else{
			pNode.next = p1;
			p1 = p1.next;
		}
		pNode = pNode.next;
	}
	if(p1 != null)
		pNode.next = p1;
	if(p2 != null)
		pNode.next = p2;
	return head;
}
// 合并两个有序链表的递归方法
public Node mergeLink2(Node head1, Node head2){
	if(head1 == null) // 检测head1是否为空
		return head2;
	else if(head2 == null) // 检测head2是否为空
		return head1;
	Node head = null;
	if(head1.data < head2.data){ 
		head = head1;
		head.next = mergeLink2(head1.next, head2); // 递归调用
	}else{
		head = head2;
		head.next = mergeLink2(head1, head2.next); // 递归调用
	}
	return head;
}

5. 判断两个链表是否相交

如果两个链表相交,那么一定有相同的尾结点。所以只要判断尾结点是否相等。

public boolean isIntersect(Node h1, Node h2){
	if(h1 == null || h2 == null)
		return false;
	Node tail1 = h1;
	while(tail1.next != null) // 找到链表h1的最后一个节点
		tail1 = tail1.next;
	Node tail2 = h2;
	while(tail2.next != null) // 找到链表h2的最后一个节点
		tail2 = tail2.next;
	return tail1 == tail2;
}

6. 找到两个链表相交的第一个节点

// 找到两个链表相交的第一个节点
public static Node getFirstMeetNode(Node h1, Node h2){
	if(h1 == null || h2 == null)
		return null;
	Node tail1 = h1;
	int len1 = 1;
	while(tail1.next != null){ // 找到链表h1的最后一个结点
		tail1 = tail1.next;
		len1++;
	}
	Node tail2 = h2;
	int len2 = 1;
	while(tail2.next != null){ // 找到链表h2的最后一个结点
		tail2 = tail2.next;
		len2++;
	}
	
	if(tail1 != tail2) // 两链表不相交
		return null;
	
	Node t1 = h1;
	Node t2 = h2;
	
	if(len1 > len2){ // 找出较长的链表先遍历
		int d = len1 - len2;
		while(d != 0){
			t1 = t1.next;
			d--;
		}
	}else{
		int d = len2 - len1;
		while(d != 0){
			t2 = t2.next;
			d--;
		}
	}
	while(t1 != t2){
		t1 = t1.next;
		t2 = t2.next;
	}
	return t1;
}

7. 检测一个链表是否有环

两个指针slow、fast一个一次走一步,一个一次走两步,如果有环,它们一定会相遇。

public boolean IsLoop(Node head){
	if(head == null || head.next == null) return false;
	Node fast = head;
	Node slow = head;
	while(fast != null && fast.next != null){
		fast = fast.next.next;  // 快指针一次走两步
		slow = slow.next; // 慢指针一次走一步
		if(fast == slow)
			return true;
	}
	return false;
}

你可能感兴趣的:(数据结构与算法)