链表相关(反转链表、合并两个有序链表、两个链表的第一个公共结点、链表入环结点、倒数第k个结点)

package Suanfa;

import java.util.*;

class ListNode {
	int val;
	ListNode next;

	ListNode(int x) {
		this.val = x;
		next = null;
	}

	int getVal() {
		return this.val;
	}
}

public class 链表 {
	public static void main(String[] args) {
		ListNode node1 = new ListNode(1);
		ListNode node2 = new ListNode(2);
		ListNode node3 = new ListNode(3);
		ListNode node4 = new ListNode(4);
		ListNode node5 = new ListNode(5);
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;
		node4.next = node5;
		ListNode node = searchK(node1, 4);
		System.out.println(node.val);
	}

	// 反转链表
	public static ListNode reverseList(ListNode head) {
		ListNode pre = null;
		if (head == null || head.next == null) {
			return head;
		}
		while (head != null) {
			ListNode temp = head;
			head = head.next;
			temp.next = pre;
			pre = temp;
		}
		return pre;
	}

	// 反转链表2,先都存到arraylist中,再反转
	public static ListNode reverseList2(ListNode head) {
		ArrayList<Integer> list = new ArrayList<>();
		while (head != null) {
			list.add(head.val);
			head = head.next;
		}
		Collections.reverse(list);
		ListNode head1 = new ListNode(-1);
		ListNode head2 = head1;
		for (int i = 0; i < list.size(); i++) {
			head1.next = new ListNode(list.get(i));
			head1 = head1.next;
		}
		return head2.next;

	}

	// 合并两个有序链表
	public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
		if (l1 == null) {
			return l2;
		}
		if (l2 == null) {
			return l1;
		}
		ListNode head = null;
		if (l1.val < l2.val) {
			head = l1;
			head.next = mergeTwoLists(l1.next, l2);
		} else {
			head=l2;
			head.next = mergeTwoLists(l1, l2.next);
		}
		return head;
	}

	// 求两个链表的第一个公共结点
	public static ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
		if (pHead1 == null || pHead2 == null) {
			return null;
		}
		ArrayList<ListNode> list = new ArrayList<>();
		while (pHead1 != null) {
			list.add(pHead1);
			pHead1 = pHead1.next;

			// 成环退出
			if (list.contains(pHead1)) {
				break;
			}
		}
		ArrayList<ListNode> list2 = new ArrayList<>();
		while (pHead2 != null) {

			// list中存在相同顶点,存在即返回
			if (list.contains(pHead2)) {
				return pHead2;
			}

			list2.add(pHead2);
			pHead2 = pHead2.next;

			// 成环退出
			if (list2.contains(pHead2)) {
				break;
			}
		}
		return null;
	}

	// 链表入环结点(hashset中已存在的成环)
	public static ListNode EntryNodeOfLoop(ListNode head) {
		HashSet<ListNode> set = new HashSet<>();
		while (head != null) {
			if (set.contains(head)) {
				return head;
			}
			set.add(head);
			head = head.next;
		}
		return null;
	}

	// 链表入环结点(快慢指针)
	public static ListNode EntryNodeOfLoop1(ListNode head) {
		ListNode fast = head, slow = head;
		while (fast != null && fast.next != null) {
			fast = fast.next.next;
			slow = slow.next;
			if (fast == slow) {
				return fast;
			}
		}
		return null;
	}

	// 倒数第k个点
	public static ListNode searchK(ListNode head, int k) {
		ListNode p = null;
		List<ListNode> list = new ArrayList<>();
		while (head != null) {
			list.add(head);
			head = head.next;
		}
		return list.get(list.size() - k);

	}

}

猜你喜欢

转载自blog.csdn.net/wenyimutouren/article/details/82228242
今日推荐