链表
public class ListNode {
//删除链表中的节点***********************************************************************************************************
int val;
ListNode next;
ListNode(int x) {
val = x;
}
/*请编写一个函数,用于 删除单链表中某个特定节点 。在设计函数时需要注意,你无法访问链表的头节点 head ,只能直接访问 要被删除的节点 。
题目数据保证需要删除的节点 不是末尾节点 。*/
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
//删除链表的倒数第N个节点**************************************************************************************************************************************************************
public ListNode get(ListNode head, int index) {
int i = 0;
ListNode hd = head;
while (i < index && hd != null) {
hd = hd.next;
i++;
}
return hd;
}
public ListNode getFromEnd(ListNode head, int n) {
ListNode fast = head;
ListNode slow = head;
for (int i = 0; i < n; i++) {
fast = fast.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null) {
return null;
}
ListNode fast = head;
ListNode slow = head;
for (int i = 0; i < n && fast != null; i++) {
fast = fast.next;
}
if (fast == null) {
return head.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
// @Test
// public void testRemoveNthFromEnd() {
// listLink.ListNode head = new listLink.ListNode(1);
// head.next = new listLink.ListNode(2);
//
// listLink.ListNode node = getFromEnd(2);
// head = removeNthFromEnd(head, 1);
// System.out.println(head.val);
// }
//反转链表***********************************************************************************************************
/* 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。*/
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode newHead = null;
while (head != null) {
//保存原链表当前头节点
ListNode node = head;
//原链表头节点后移
head = head.next;
//把保存的节点下一个指向新链表的头节点,拆除
node.next = newHead;
//从原链表上删除 的节点变成新节点的头节点
newHead = node;
}
return newHead;
}
//合并两个有序链表***********************************************************************************************************
/* 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode root = new ListNode(0);
//尾节点
ListNode tail = root;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
//把符合要求的节点拼接在尾节点后面
tail.next = l1;
//新品姐的节点变成尾节点
tail = l1;
//被拼接的原链表指针后移
l1 = l1.next;
} else {
tail.next = l2;
tail = l2;
l2 = l2.next;
}
}
if (l1 != null) {
tail.next = l1;
}
if (l2 != null) {
tail.next = l2;
}
return root.next;
}
//输出链表中间节点***********************************************************************************************************
public ListNode halfNode(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null) {
slow = slow.next;
fast = fast.next;
if (fast == null) {
//奇数
return slow;
}
fast = fast.next;
}
return slow;
}
//回文链表***********************************************************************************************************
public boolean isPalindrome(ListNode head) {
ListNode fast = head, slow = head;
ListNode leftHead = null;
boolean isOdd = true;
while (fast.next != null) {
fast = fast.next;
ListNode node = slow;
slow = slow.next;
node.next = leftHead;
leftHead = node;
if (fast.next == null) {
// 偶数
System.out.println("偶数");
// 不是奇数
isOdd = false;
break;
}
fast = fast.next;
}
if (isOdd) {
slow = slow.next;
}
while (leftHead != null) {
if (leftHead.val != slow.val) {
return false;
}
leftHead = leftHead.next;
slow = slow.next;
}
return true;
}
// public boolean isPalindrome(ListNode head) {
// if (head.next == null) {
// return true;
// }
// Stack<ListNode> stack = new Stack<ListNode>();
// stack.push(head);
// while (head.next != null) {
// if (head.next.val > stack.lastElement().val) {
// stack.push(head.next);
// } else {
// stack.pop();
// }
// head = head.next;
// }
// return stack.isEmpty();
// }
//数组算法********************************************************************************************************************
public boolean isPalindrome2(ListNode head) {
ArrayList<ListNode> list = new ArrayList();
while (head != null) {
list.add(head);
head = head.next;
}
int start = 0;
int end = list.size() - 1;
while (start <= end && (list.get(start).val == list.get(end).val)) {
start++;
end--;
}
return true;
}
//
// public boolean isPalindrome3(ListNode head) {
// if (head.next == null) {
// return true;
// }
// frontPointer = head;
// return recursivelyCheck(head);
// }
// private ListNode frontPointer;
// private boolean recursivelyCheck(ListNode node) {
// if (node != null) {
// if (!recursivelyCheck(node.next)) {
// return false;
// }
// if (node.val != frontPointer.val) {
// return false;
// }
// frontPointer = frontPointer.next;
// }
// return true;
// }
//
// public boolean isPalindrome4(ListNode head) {
// ListNode fast = head;
// ListNode slow = head;
// Stack<ListNode> stack = new Stack<ListNode>();
// //通过快慢指针找到中点
// while (fast != null && fast.next != null) {
// fast = fast.next.next;
// stack.push(slow);
// slow = slow.next;
// }
// //如果fast不为空,说明链表的长度是奇数个
// if (fast != null) {
// slow = slow.next;
// }
// while (slow != null) {
// if (slow.val != stack.pop().val) {
// return false;
// }
// slow = slow.next;
// }
// return true;
// }
//
// public boolean isPalindrome5(ListNode head) {
// ListNode fast = head, slow = head;
// //通过快慢指针找到中点
// while (fast != null && fast.next != null) {
// fast = fast.next.next;
// slow = slow.next;
// }
// //如果fast不为空,说明链表的长度是奇数个
// if (fast != null) {
// slow = slow.next;
// }
// //反转后半部分链表
// slow = reverse(slow);
//
// fast = head;
// while (slow != null) {
// //然后比较,判断节点值是否相等
// if (fast.val != slow.val)
// return false;
// fast = fast.next;
// slow = slow.next;
// }
// return true;
// }
//
// //反转链表
// public ListNode reverse(ListNode head) {
// ListNode root = null;
// while (head != null) {
// ListNode next = head.next;
// head.next = root;
// root = head;
// head = next;
// }
// return root;
// }
}
//环形链表******************************************************************************************************************
public boolean hasCycle(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next;
if (fast == null) {
break;
}
fast = fast.next;
if (fast == slow) {
return true;
}
}
return false;
}
//2.
public boolean hasCycle2(ListNode head) {
while (head != null) {
if (head.next == head) {
return true;
}
ListNode node = head;
head = head.next;
node.next = node;
}
return false;
}
}

浙公网安备 33010602011771号