目录
141.环形链表
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) return false;
ListNode slow = head;
ListNode fast = head;
while (fast != null) {
slow = slow.next;
if (fast.next == null) return false;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
}
142.环形链表 II
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) return null;
ListNode slow = head;
ListNode fast = head;
while (fast != null) {
slow = slow.next;
if (fast.next == null) return null;
fast = fast.next.next;
if (slow == fast) break;
}
if (fast == null) return null;
while (head != fast) {
head = head.next;
fast = fast.next;
}
return head;
}
}
21.合并两个有序链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
ListNode pre = new ListNode(0);
ListNode node = pre;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
node.next = list1;
list1 = list1.next;
node = node.next;
} else {
node.next = list2;
list2 = list2.next;
node = node.next;
}
}
if (list1 == null) node.next = list2;
else if (list2 == null) node.next = list1;
return pre.next;
}
}
206.反转链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
// public ListNode reverseList(ListNode head) {
// // 迭代
// if (head == null) return head;
// ListNode node = head;
// head = head.next;
// node.next = null;
// while (head != null) {
// ListNode temp = head.next;
// head.next = node;
// node = head;
// head = temp;
// }
// return node;
// }
public ListNode reverseList(ListNode head) {
// 递归
if (head == null || head.next == null) return head;
ListNode node = head.next;
ListNode reverseHead = reverseList(node);
node.next = head;
head.next = null;
return reverseHead;
}
}
24.两两交换链表中的节点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode node = head.next.next;
ListNode swapHead = head.next;
head.next.next = head;
head.next = node;
while (node != null && node.next != null) {
ListNode temp = node;
node = node.next.next;
head.next = temp.next;
temp.next.next = temp;
temp.next = node;
head = temp;
}
return swapHead;
}
}
160.相交链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode node1 = headA;
ListNode node2 = headB;
while (node1 != node2) {
if (node1 == null) node1 = headB;
else node1 = node1.next;
if (node2 == null) node2 = headA;
else node2 = node2.next;
}
return node1;
}
}
83.删除排序链表中的重复元素
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head;
ListNode node = head;
while (node.next != null) {
if (node.val == node.next.val) node.next = node.next.next;
else node = node.next;
}
return head;
}
}
19.删除链表的倒数第 N 个结点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head.next == null) return null;
ListNode fast = head;
ListNode slow = head;
for (int i = 0; i < n; 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;
}
}
445.两数相加 II
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<ListNode> stack1 = new Stack<>();
Stack<ListNode> stack2 = new Stack<>();
buildStack(stack1, l1);
buildStack(stack2, l2);
ListNode node = null;
int m = 0;
while (!stack1.empty() || !stack2.empty() || m != 0) {
int a = stack1.empty() ? 0 : stack1.pop().val;
int b = stack2.empty() ? 0 : stack2.pop().val;
int c = (a + b + m) / 10;
int d = (a + b + m) % 10;
m = c;
ListNode newNode = new ListNode(d);
newNode.next = node;
node = newNode;
}
return node;
}
public void buildStack(Stack<ListNode> stack, ListNode node) {
while (node != null) {
stack.push(node);
node = node.next;
}
}
}
234.回文链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head.next == null) return true;
ListNode fast = head.next;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode rNode = null;
if (fast == null) rNode = reverseNode(slow);
else rNode = reverseNode(slow.next);
while (rNode != null) {
if (rNode.val != head.val) return false;
rNode = rNode.next;
head = head.next;
}
return true;
}
public ListNode reverseNode(ListNode head) {
if (head.next == null) return head;
ListNode node = null;
while (head != null) {
ListNode temp = head.next;
head.next = node;
node = head;
head = temp;
}
return node;
}
}
328.奇偶链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null) return head;
ListNode oddHead = head;
ListNode evenHead = head.next;
while (head.next != null) {
ListNode temp = head.next;
head.next = temp.next;
if (head.next == null) break;
head = head.next;
temp.next = head.next;
}
head.next = evenHead;
return oddHead;
}
}
725.分隔链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode[] splitListToParts(ListNode head, int k) {
ListNode[] result = new ListNode[k];
if (head == null) return result;
int nodes = sumNodes(head);
int m = nodes / k;
int n = nodes % k;
int[] arr = new int[k];
Arrays.fill(arr, m);
for (int i = 0; i < n; i++) {
arr[i] += 1;
}
for (int i = 0; i < k; i++) {
if (arr[i] == 0) continue;
result[i] = head;
ListNode pre = null;
for (int j = 0; j < arr[i]; j++) {
pre = head;
head = head.next;
}
pre.next = null;
}
return result;
}
public int sumNodes(ListNode head) {
int sum = 0;
while (head != null) {
sum++;
head = head.next;
}
return sum;
}
}