题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/description/
整体思路:使用虚拟头节点为保证处理的一致性,添加虚拟头节点,并一个节点的处理作为一个循环体,大致移动过程和如下图:
Java实现代码:
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode v = new ListNode(0,head);//step1
ListNode cur = v;
ListNode temp1;
ListNode temp2;
ListNode temp3;
while((cur.next !=null)&&(cur.next.next != null)){
temp1 = cur.next;//step2
temp2 = cur.next.next;//step3
temp3 = cur.next.next.next;//step4
cur.next = temp2;//step5
temp2.next = temp1;//step6
temp1.next = temp3;//step7
cur = temp1;//cur移动到下一个点
}
return v.next;//除去虚拟头节点
}
}
题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/description/
eg:head = [1,2,3,4,5], n = 2
思路:双指针思想+虚拟头节点
slow fast两个指针,初始化分别指向虚拟头节点V;移动找到第倒数第n+1个节点(slow)
1.向前n步移动fast 2.slow 与fast 同步移动,直到fast指向最后一个节点即fast.next = null结束
3.再删除slow下面的节点,即可实现
Java实现代码:
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode v = new ListNode(0,head);
ListNode slow = v;
ListNode fast = v;
for(int i=0 ;i < n;i++){
fast = fast.next;
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return v.next;
}
}
题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/description/
分析:需要注意,判断链表相交是以节点地址值是否相等位依据,不是node.val
以这个示例进行分析:A[4,1,8,4,5] B[5,0,1,8,4,5]
标题 Leetcode19:两链表相交:例子数据:A[4,1,8,4,5] B[5,0,1,8,4,5]Java实现代码
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lengthA = 0;
int lengthB = 0;
ListNode curL = headA;
ListNode curS = headB;
//求两个链表长度,此时curL 用于求A,curS用于求B;
while(curL != null){
lengthA++;
curL = curL.next;
}
while(curS != null){
lengthB++;
curS = curS.next;
}
//两个指针复原curS指向较短的链表,curL指向较长的链表
if(lengthA>=lengthB){
curL = headA;
curS = headB;
}else{
curL = headB;
curS = headA;
}
//后端对其
//lengthDiff链表长度差值
int lengthDiff =Math.abs(lengthA-lengthB);
while(lengthDiff != 0){
curL =curL.next;
lengthDiff--;
}
//找到相等地址节点,即交汇点
while(curL != curS){
curL = curL.next;
curS = curS.next;
if(curL==null) return null;
}
return curL;
}
}
代码书写注意点:两个指针curL,curS用了两次,一次用于求链表长度,一次用于后续查找交点,所以第二次使用前记得将两个指针重新进行初始化
Java代码:解析文章:https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html
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;
}
}
我看了以上文章解题思路后写代码进行了实现,但是在力扣没有通过,看了好久没有找到问题的原因,希望哪位朋友能看出问题,帮忙指出,逻辑问题或者代码问题都可,我把我的错误代码及错误问题截图放在了下面,期待大家批评指正,十分感谢:
Java 有问题未解决的代码:
/**
* 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 != slow){
fast = fast.next.next;
slow = slow.next;
if((fast.next==null)||(fast==null)){
return null;
}
}
ListNode index1 = fast;
ListNode index2 = head;
while(index1 != index2){
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}