提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
/**
* 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 trainingPlan(ListNode head, int cnt) {
ListNode p1 = head, p2;
for(int i = 0; i < cnt; i ++){
p1 = p1.next;
}
p2 = head;
while(p1 != null){
p1 = p1.next;
p2 = p2.next;
}
return p2;
}
}
/**
* 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 trainningPlan(ListNode l1, ListNode l2) {
ListNode dump = new ListNode(-1, null), pre = dump;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
pre.next = l1;
pre = pre.next;
l1 = l1.next;
}else{
pre.next = l2;
pre = pre.next;
l2 = l2.next;
}
}
if(l1 != null){
pre.next = l1;
}
if(l2 != null){
pre.next = l2;
}
return dump.next;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA, p2 = headB;
while(p1 != p2){
if(p1 == null) p1 = headB;
else p1 = p1.next;
if(p2 == null) p2 = headA;
else p2 = p2.next;
}
return p1;
}
}
/**
* 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) {
ListNode p = new ListNode(-1,head);
ListNode r = fun(p,n+1);
r.next = r.next.next;
return p.next;
}
public ListNode fun(ListNode head, int n){
ListNode p = head;
int i = 1;
while(i < n){
i ++;
p = p.next;
}
ListNode res = head;
while(p.next != null){
p = p.next;
res = res.next;
}
return res;
}
}
/**
* 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) {
ListNode fast = head, slow = head;
while(slow != null && fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
return null;
}
}