package com.zte.array1;
import java.util.HashSet;
public class Main3 {
public static void main(String[] args) {
}
public static ListNode delete(ListNode head) {
HashSet<Integer> set = new HashSet<>();
ListNode currrent = null;
ListNode parent = head;
while (parent != null) {
if (set.contains(parent.val)) {
currrent.next = parent.next;
} else {
set.add(parent.val);
currrent = parent;
parent = parent.next;
}
}
return head;
}
public static int kthToLast(ListNode head, int k) {
ListNode front = head;
ListNode after = head;
if (head == null && head.next != null) {
return -1;
} else {
for (int i = 0; i < k; i++) {
front = front.next;
}
while (front != null) {
front = front.next;
after = after.next;
}
}
return after.val;
}
public static void deleteNode(ListNode node) {
if (node != null && node.next != null) {
ListNode nodeNext = node.next;
node.val = nodeNext.val;
node.next = nodeNext.next;
}
}
public static ListNode partition(ListNode head, int x) {
ListNode before = null;
ListNode after = null;
while (head != null) {
if (x > head.val) {
if (before == null) {
before = head;
head = head.next;
} else {
ListNode end = before;
while (end.next != null)
end = before.next;
end.next = head;
head = head.next;
}
} else {
if (after == null) {
after = head;
head = head.next;
} else {
ListNode end = after;
while (end.next != null)
end = end.next;
end.next = head;
head = head.next;
}
}
}
while (before.next != null) {
before = before.next;
}
before.next = after;
return before;
}
public static ListNode partition1(ListNode head, int x) {
ListNode beforestart = null;
ListNode beforeend = null;
ListNode afterstart = null;
ListNode afterend = null;
while (head != null) {
if (x > head.val) {
if (beforestart == null) {
beforestart = head;
beforeend = head;
} else {
beforeend.next = head;
beforeend = head;
}
head = head.next;
} else {
if (afterstart == null) {
afterstart = head;
afterend = head;
} else {
afterend.next = head;
afterend = head;
}
head = head.next;
}
}
if (beforeend == null)
return afterstart;
else {
beforeend.next = afterstart;
return beforestart;
}
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
package com.zte.array1;
import java.util.Stack;
public class Main4 {
public static void main(String[] args) {
int n = 12;
int[][] graph = { { 0, 1 }, { 1, 2 }, { 1, 3 }, { 1, 10 }, { 1, 11 }, { 1, 4 }, { 2, 4 }, { 2, 6 }, { 2, 9 },
{ 2, 10 }, { 2, 4 }, { 2, 5 }, { 2, 10 }, { 3, 7 }, { 3, 7 }, { 4, 5 }, { 4, 11 }, { 4, 11 }, { 4, 10 },
{ 5, 7 }, { 5, 10 }, { 6, 8 }, { 7, 11 }, { 8, 10 } };
int start = 2;
int target = 3;
System.out.println(findWhetherExistsPath(n, graph, start, target));
}
public static boolean findWhetherExistsPath(int n, int[][] graph, int start, int target) {
int[][] arr = new int[n][n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
int k = graph[i][0];
int l = graph[i][1];
arr[k][l] = 1;
}
return isVisitied1(n, arr, start, target, stack);
}
public static boolean isVisitied(int n, int[][] arr, int start, int target, Stack<Integer> stack) {
boolean[] isvisited = new boolean[n];
isvisited[start] = true;
for (int i = 0; i < n; i++) {
if (!isvisited[i] && arr[start][i] != 0 && target == i) {
return true;
}
if (!isvisited[i] && arr[start][i] != 0 && target != i) {
stack.push(i);
}
}
if (stack.size() != 0) {
isVisitied(n, arr, stack.pop(), target, stack);
}
return false;
}
public static boolean findWhetherExistsPath1(int n, int[][] graph, int start, int target) {
int[][] arr = new int[n][n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
int k = graph[i][0];
int l = graph[i][1];
arr[k][l] = 1;
arr[l][k] = 1;
}
return isVisitied(n, arr, start, target, stack);
}
public static boolean isVisitied1(int n, int[][] arr, int start, int target, Stack<Integer> stack) {
boolean[] isvisited = new boolean[n];
isvisited[start] = true;
stack.push(start);
while (stack.size() != 0) {
int k = stack.pop();
System.out.print(k + " ->");
isvisited[k] = true;
for (int i = 0; i < n; i++) {
if (!isvisited[i] && arr[k][i] != 0 && i == target) {
return true;
}
if (!isvisited[i] && arr[k][i] != 0 && i != target) {
stack.push(i);
}
}
}
return false;
}
public static boolean isVisitied2(int n, int[][] arr, int start, int target, Stack<Integer> stack) {
boolean[] isvisited = new boolean[n];
isvisited[start] = true;
stack.push(start);
while (stack.size() != 0) {
int k = stack.pop();
System.out.print(k + " ->");
isvisited[k] = true;
for (int i = 0; i < n; i++) {
if (!isvisited[i] && arr[k][i] != 0 && i == target) {
return true;
}
if (!isvisited[i] && arr[k][i] != 0 && i != target) {
stack.push(i);
}
}
}
return false;
}
}