重写了设计链表,注意LinkedList是java里面的专属类,不能用这个名字
/**
* 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(-1);
dummyHead.next = head;
ListNode cur = dummyHead;
while(cur.next!=null&&cur.next.next!=null){
//node结点数为奇数和偶数的判断条件
ListNode firNode =cur.next;
ListNode scNode = cur.next.next;
ListNode temp = cur.next.next.next;
cur.next = scNode;
//忘记虚拟头节点了,这一步是最关键一步
scNode.next = firNode;
firNode.next = temp;
cur = firNode;
}
return dummyHead.next;
}
}
很久没画图了,还是画个图理一下思路吧
画图可以用这个网站:
Excalidraw | Hand-drawn look & feel • Collaborative • Secure
|19.删除链表的倒数第N个节点
/**
* 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;
while(n>0){
fast = fast.next;
n--;
}
while(fast.next!=null){//detail
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummyHead.next;
}
}
用while不要用正向思维,用逆向思维,你想的是让fast.next==null
->while(fast.next!=null) 这样思路会清晰的多
面试题02.07链表相交
整体思路就是比对两个链表,我们可以发现
链表在交点之后的后半部分都是相同的
前半部分虽然不同,但是却在交点部分对齐了。
我们分别计算两个链表长度,得到差值
这个差值就是我们的操作指针在长链表中的移动距离
还有就是,兄弟们,注意这题测试用例
intersectVal = 8,
listA = [4,1,8,4,5]
listB = [5,0,1,8,4,5]
skipA = 2
skipB = 3
里面有个2,3,所以说他要求A链表相交前必须有2个结点,B必须有3个结点,不是地址问题。
如果题目没有这两个条件,8就是相交点
/**
* 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 curA = headA;
ListNode curB = headB;
int lenA = 0,lenB = 0;
while(curA!=null){
curA = curA.next;
lenA ++;
}
while(curB!=null){
curB =curB.next;
lenB++;
}
curA = headA;
curB = headB;
//我们固定认为lenA<lenB
if(lenA>lenB){
int temp = lenA;
lenA = lenB;
lenB = temp;
ListNode temp2 = curA;
curA = curB;
curB = temp2;
}
int gap = lenB-lenA;
while(gap-->0){
curB = curB.next;
}
while(curA!=null){
if(curA==curB){
return curA;
}
else{
curA = curA.next;
curB = curB.next;
}
}
return null;
}
}
142.环形链表
我们的n是fast指针走的圈数,那么y+z相当于原点嘛,x=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 fast = head;
ListNode slow = head;
while(fast!=null&&fast.next!=null){
fast = fast.next.next;
slow = slow.next;
if(fast==slow){
ListNode index1 = fast;
ListNode index2 = head;
while(index1!=index2){
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}