其实并不是很难,但是递归的还是很难
简单版
/**
* 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) {
//设置虚拟头节点且指向头节点
ListNode dumHead=new ListNode(-1);
dumHead.next=head;
ListNode cur = dumHead;
while(cur.next!=null&&cur.next.next!=null)
//当链表为偶/奇数个,遍历到后面为空时停止
{
ListNode temp=cur.next;
ListNode temp1=cur.next.next.next;
cur.next=cur.next.next;
cur.next.next=temp;
temp.next=temp1;
cur=cur.next.next;
}
return dumHead.next;
}
}
递归版(来自代码随想录)
// 递归版本
class Solution {
public ListNode swapPairs(ListNode head) {
// base case 退出提交
if(head == null || head.next == null) return head;
// 获取当前节点的下一个节点
ListNode next = head.next;
// 进行递归
ListNode newNode = swapPairs(next.next);
// 这里进行交换
next.next = head;
head.next = newNode;
return next;
}
}
/**
* 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 newHead=new ListNode(0);
newHead.next=head;
ListNode slow=newHead;
ListNode fast=newHead;
//将快指针向后移动与满指针间隔n-1个位置
//当快指针指向null,慢指针与其间隔n个,则慢指针正好指向要删除的节点
//这样当快指针指向空的时候,满指针指向的是要删除的节点的前一个位置
for(int i=0;i<n;i++){
//快慢指针间隔n-1,只要满足这个条件,慢指针就可以正确指向
fast=fast.next;
}
while(fast.next!=null){
//快慢指针同时移动
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return newHead.next;
}
}
这里不是从头开始判断是否有相同的,而是先将尾部对齐
/**
* 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 cruA=headA;
ListNode cruB=headB;
//Len值链表长度
int LenA=0;
int LenB=0;
while(cruA!=null){
LenA++;
cruA=cruA.next;
}
while(cruB!=null){
LenB++;
cruB=cruB.next;
}
cruA=headA;
cruB=headB;
if(LenB>LenA){
int temp=LenB;
LenB=LenA;
LenA=temp;
ListNode tempC=cruB;
cruB=cruA;
cruA=tempC;
}
//求长度差
int length=LenA-LenB;
//让两个指针分别在单链表,使之末尾对齐的状态
while(length-- >0){
cruA=cruA.next;
}
while(cruA!=null){
if(cruA==cruB){
return cruA;
}
cruA=cruA.next;
cruB=cruB.next;
}
return null;
}
}
应该可以用hash表来做,but是链表的章节
代码随想录里面有详细的图解
重要的是Z的大小,决定了入口处
/**
* 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 slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {// 相遇
ListNode index1 = fast;
ListNode index2 = head;
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}