剑指 Offer II 021. 删除链表的倒数第 n 个结点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1, head);
ListNode pre = dummy, p = head;
for (int i = 0; i < n; i++) {
p = p.next;
}
while (p != null) {
p = p.next;
pre = pre.next;
}
pre.next = pre.next.next;
return dummy.next;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
Set<ListNode> set = new HashSet<>();
ListNode p = head;
while (p != null) {
if (set.contains(p)) {
return p;
}
set.add(p);
p = p.next;
}
return null;
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> set = new HashSet<>();
ListNode p = headA;
while (p != null) {
set.add(p);
p = p.next;
}
p = headB;
while (p != null) {
if (set.contains(p)) {
return p;
}
p = p.next;
}
return null;
}
}
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode pre = head, p = head.next;
while (p != null) {
ListNode t = p.next;
p.next = pre;
pre = p;
p = t;
}
head.next = null;
return pre;
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode l1_r = reverseList(l1);
ListNode l2_r = reverseList(l2);
ListNode s_r = sum(l1_r, l2_r);
return reverseList(s_r);
}
public ListNode reverseList(ListNode head) {
ListNode pre = head, p = head.next;
while (p != null) {
ListNode t = p.next;
p.next = pre;
pre = p;
p = t;
}
head.next = null;
return pre;
}
public ListNode sum(ListNode l1, ListNode l2) {
ListNode head1 = l1, head2 = l2;
int c = 0;
ListNode res = new ListNode(0);
ListNode p = res;
while (head1 != null || head2 != null) {
int n1 = head1 == null ? 0 : head1.val;
int n2 = head2 == null ? 0 : head2.val;
int sum = n1 + n2 + c;
p.next = new ListNode(sum % 10);
p = p.next;
c = sum / 10;
if (head1 != null) {
head1 = head1.next;
}
if (head2 != null) {
head2 = head2.next;
}
}
if (c > 0) {
p.next = new ListNode(c);
}
return res.next;
}
}
class Solution {
public void reorderList(ListNode head) {
List<ListNode> list = new ArrayList<>();
ListNode p = head;
while (p != null) {
list.add(p);
p = p.next;
}
int left = 0, right = list.size() - 1;
while (left < right) {
ListNode t = list.get(left).next;
list.get(left).next = list.get(right);
list.get(right).next = t;
left++;
right--;
}
list.get(left).next = null;
}
}
class Solution {
public boolean isPalindrome(ListNode head) {
List<ListNode> list = new ArrayList<>();
ListNode p = head;
while (p != null) {
list.add(p);
p = p.next;
}
int l = 0, r = list.size() - 1;
while (l < r) {
if (list.get(l).val != list.get(r).val) {
return false;
}
l++;
r--;
}
return true;
}
}
class Solution {
public Node flatten(Node head) {
flattenGetTail(head);
return head;
}
public Node flattenGetTail(Node head) {
Node node = head;
Node tail = null;
while (node != null) {
Node next = node.next;
if (node.child != null) {
Node child = node.child;
Node childTail = flattenGetTail(child);
node.child = null;
node.next = child;
child.prev = node;
childTail.next = next;
if (next != null) {
next.prev = childTail;
}
tail = childTail;
} else {
tail = node;
}
node = next;
}
return tail;
}
}
class Solution {
public Node insert(Node head, int insertVal) {
if (head == null) {
head = new Node(insertVal);
head.next = head;
return head;
}
if (head.next == null) {
head.next = new Node(insertVal, head);
return head;
}
Node curr = head, next = head.next, biggest = head;
while (next != head && !(curr.val <= insertVal && next.val >= insertVal)) {
curr = curr.next;
next = next.next;
if (biggest.val <= curr.val) {
biggest = curr;
}
}
if (curr.val <= insertVal && next.val >= insertVal) {
curr.next = new Node(insertVal, next);
} else {
Node t = new Node(insertVal, biggest.next);
biggest.next = t;
}
return head;
}
}