24两两交换链表中的节点
题目链接:24. 两两交换链表中的节点 - 力扣(LeetCode)
难点:设置基准点,本题的基准点是要交换的两个节点前一个节点。要注意赋值顺序,利用变量保存节点。
/**
* 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 dummyhead=new ListNode();
dummyhead.next=head;
ListNode cur=dummyhead;
while(cur.next!=null&&cur.next.next!=null){
ListNode pre=cur.next;
ListNode last=cur.next.next.next;
cur.next=cur.next.next;
cur.next.next=pre;
pre.next=last;
cur=cur.next.next;
}
return dummyhead.next;
}
}
19删除链表的倒数第N个节点
题目链接:19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
难点:运用快慢指针,让快指针先走N+1步,之后快慢指针同时向前。本题的基准点是倒数第N+1个结点。
/**
* 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 dummyhead=new ListNode();
dummyhead.next=head;
ListNode fast=dummyhead;
ListNode slow=dummyhead;
int loop=n+1;
while(fast!=null&&loop-->0){
fast=fast.next;
}
while(fast!=null){
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return dummyhead.next;
}
}
142环形链表
题目链接:142. 环形链表 II - 力扣(LeetCode)
难点:运用快慢指针。快指针走两步,慢指针走一步,当 快慢指针相遇时,表示存在环。此时再设置一个指针指向头节点,与慢指针以相同步数前进,再次相遇时即是环的起始。
/**
* 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;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
ListNode index=head;
while(slow!=index){
slow=slow.next;
index=index.next;
}
return index;
}
}
return null;
}
}
面试题0207链表相交
题目链接:面试题 02.07. 链表相交 - 力扣(LeetCode)
思路:找出更长的一组,设置两个指针分别指向两个链表,让长的链表先走len=lenA-lenB步,之后两个指针同时移动,两个指针所指节点相同的既是相交的第一个节点。
/**
* 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) {
if(headA==null||headB==null){
return null;
}
ListNode curA=headA;
ListNode curB=headB;
int lenA=0;
int lenB=0;
int gap=0;
while(curA!=null){
curA=curA.next;
lenA++;
}
while(curB!=null){
curB=curB.next;
lenB++;
}
// 让指针重新指向头节点
curA=headA;
curB=headB;
if(lenA<lenB){
int templ=lenA;
lenA=lenB;
lenB=templ;
ListNode temp=curA;
curA=curB;
curB=temp;
}
gap=lenA-lenB;
while(gap-->0){
curA=curA.next;
}
while(curA!=null){
if(curA==curB){
return curA;
}
curA=curA.next;
curB=curB.next;
}
return null;
}
}
/**
* 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) {
if(headA==null||headB==null){
return null;
}
ListNode curA=headA;
ListNode curB=headB;
while(curA!=curB){
curA=curA==null?headB:curA.next;
curB=curB==null?headA:curB.next;
}
return curA;
}
}