@Data
public class ListNode {
private int val;
private ListNode next;
void insert(ListNode n0, ListNode target) {
target.next = n0.next;
n0.next = target;
}
void delete(ListNode n) {
if (n == null || n.next == null) return;
n.next = n.next.next;
}
ListNode access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
if (head == null) return null;
head = head.next;
}
return head;
}
ListNode search(ListNode head, int target) {
while (head != null) {
if (head.val == target) {
return head;
}
head = head.next;
}
return head;
}
void iter(ListNode head) {
while (head != null) {
System.out.println(head.val);
head = head.next;
}
}
ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
ListNode reverserRecursion(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newNode = reverserRecursion(head.next);
head.next.next = head;
head.next = null;
return newNode;
}
}