链表——写给面试的本身

package offer.linklist;

public class LinkList {
	// 头结点
	public Node head;
	// 当前结点
	public Node current;

	// 添加结点
	public void add(int data) {
		// 判断链表是否为null
		if (head == null) {
			// 链表为null,则表示链表中尚未结点
			// 头结点为新加入结点
			head = new Node(data);
			// 当前的结点变为头结点
			current = head;
		} else {
			// 链表不为null,则当前结点的下一个结点为新加入结点
			current.next = new Node(data);
			// 当前的结点变为新加入的结点
			current = current.next;// 这一步是关键,别忘!!!!
		}
	}
}
package offer.linklist;

public class Node {
	public int data;
	public Node next;

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

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return String.valueOf(data);
	}
}
package offer.linklist;
public class Test {
	// 遍历链表
	public static void printLinkList(LinkList list) {
		if (list != null) {
			Node current = list.head;
			while (current != null) {
				System.out.print("--->" + current.data);
				current = current.next;
			}
		}
	}
	// 链表的结点数
	public static int NodeNum(LinkList list) {
		if (list == null || list.head == null) {
			return 0;
		}
		int count = 0;
		Node current = list.head;
		while (current != null) {
			count++;
			current = current.next;
		}
		return count;
	}
	// 查找链表中第k个结点
	public static Node findNode(LinkList list, int k) {
		if (list == null || list.head == null) {
			return null;
		}
		Node current = list.head;
		int count = 1;
		while (current != null) {
			if (count == k) {
				return current;
			}
			count++;
			current = current.next;
		}
		return null;
	}
	// 查找链表中倒数第k个结点(方法一)
	public static Node findLastNode1(LinkList list, int k) {
		if (list == null || list.head == null) {
			return null;
		}
		int nodeNum = NodeNum(list);
		if (k < nodeNum) {
			return findNode(list, nodeNum - k + 1);
		}
		return null;
	}
	// 查找链表中倒数第k个结点(方法二)
	public static Node findLastNode2(LinkList list, int k) {
		if (list == null || list.head == null) {
			return null;
		}
		// 第一个标记
		Node frist = list.head;
		// 第二个标记
		Node second = list.head;
		int count = 0;
		// 将第二个标记移动到k个结点
		while (count != k && second != null) {
			count++;
			second = second.next;
		}
		if (count != k) {
			return null;
		}
		// 将第一个和第二个标记日后移动,直到第二个标记达到最后一个结点,则第一个标记的结点就是咱们要找的结点
		while (second != null && frist != null) {
			frist = frist.next;
			second = second.next;
		}
		return frist;
	}
	// 查找链表的中间结点
	public static Node findMidNode(LinkList list) {
		if (list == null || list.head == null) {
			return null;
		}
		Node frist = list.head;
		Node second = list.head;
		// frist 走一步 second 走两步 直到second走到最后一个结点
		while (frist != null && second != null && second.next != null) {
			frist = frist.next;
			second = second.next.next;
		}
		return frist;
	}
	// 合并两个有序链表,合并后仍然有序
	public static LinkList mergeOrderedLinkList(LinkList list1, LinkList list2) {
		if (list1 == null || list1.head == null) {
			return list2;
		} else if (list2 == null || list2.head == null) {
			return list1;
		}
		LinkList list = new LinkList();
		if (list1.head.data > list2.head.data) {
			list.head = list2.head;
			list2.head = list2.head.next;
		} else {
			list.head = list1.head;
			list1.head = list1.head.next;
		}
		// 当前结点
		Node current = list.head;
		while (current != null && list1.head != null && list2.head != null) {
			if (list1.head.data < list2.head.data) {
				current.next = list1.head;
				list1.head = list1.head.next;
			} else {
				current.next = list2.head;
				list2.head = list2.head.next;
			}
			current = current.next;
		}
		if (list1.head != null) {
			current.next = list1.head;
		}
		if (list2.head != null) {
			current.next = list2.head;
		}
		return list;
	}
	// 反转链表 面试出现率最高
	public static LinkList reverseLinkList(LinkList list) {
		if (list == null || list.head == null || list.head.next == null) {
			return list;
		}
		Node current = list.head;// 旧链表的头结点
		Node next;// 用来保存下一下结点
		LinkList linkList = new LinkList();
		while (current != null) {
			next = current.next;
			current.next = linkList.head;// 关键代码
			linkList.head = current;// 关键代码
			current = next;
		}
		return linkList;
	}
	public static void main(String[] args) {
		LinkList list01 = new LinkList();
		for (int i = 0; i < 5; i++) {
			list01.add(i);
		}
		printLinkList(reverseLinkList(list01));
		// System.out.println(NodeNum(list01));
		// System.out.println("=================");
		// printLinkList(list01);
		// System.out.println();
		// System.out.println("=================");
		// System.out.println(findNode(list01, 2));
		// System.out.println("=================");
		// System.out.println(findLastNode1(list01, 2));
		// System.out.println("=================");
		// System.out.println(findLastNode2(list01, 2));
		// System.out.println(findLastNode2(list01, 8));
		// System.out.println("=================");
		// System.out.println(findMidNode(list01));
		// System.out.println("=================");
		// LinkList list02 = new LinkList();
		// for (int i = 0; i < 10; i = i + 2) {
		// list02.add(i);
		// }
		// printLinkList(list02);
		// System.out.println();
		// System.out.println("=================");
		// printLinkList(mergeOrderedLinkList(list01, list02));//
		// printLinkList(mergeOrderedLinkList(list01,
		// // null));
		// System.out.println();
		// System.out.println("=================");
	}
}
相关文章
相关标签/搜索