代码随想录算法训练营第4天:24两两交换链表节点;19删除链表的倒数第N个结点;链表相交;142环形链表2

24两两交换链表节点;

大胆去写即可,先理清思路,然后大胆去写,注意缓存的数据,要有两个指针。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
    ListNode *dummyhead = new ListNode(0);
    dummyhead -> next = head;
    ListNode *cur = dummyhead; 
    while(cur->next != nullptr && cur->next->next != nullptr)
    {  //不要改变虚拟头节点 所以用cur指针来替代  并且不用释放
        //ListNode *temp = new ListNode(0);
        ListNode *temp = cur->next;
        ListNode *temp1 = cur->next->next->next;
        
        cur->next = cur->next->next;  //yibu 头节点指向2相当于断开了1
        cur->next->next = temp;   //dierbu
        
        
        
        cur->next->next->next = temp1;    //第三步 将原来的节点2变为temp1  即使得2这个位置可以临时记录4  下一阶段开始前记录  不然等cur执行完下一行 移动完2位后  值就变了 就相当于移动5位了
   //cur为指针 不需要释放  
        cur = cur->next->next;  //cur移动两位
        
        
    }
        return dummyhead->next;
    }
};

19 

两种方法 1,都

很巧妙  这是先遍历求得长度 然后倒数第n个元素 就是遍历到l-n+1的位置;、

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
   int getLength(ListNode* head) {
        int length = 0;
        while (head) {
            ++length;
            head = head->next;
        }
        return length;
    }
    ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode *dummyhead = new ListNode(0,head);
    int length = getLength(head);
    
    ListNode *cur = dummyhead;   //声明cur指针等于dummyhead ,指向dummyhead所指向的结点 只是一个遍历指针
    
    //当遍历到第 L−n+1个节点  L-n就是倒数第n个节点的前一个节点,所以再加一即是n这个位置的节点
    for (int i =1;i<length-n+1;++i)  //从第一个结点开始遍历 所以i先加一
    {
    cur = cur->next;
    }
    cur ->next = cur ->next->next;
    return dummyhead->next;
    delete dummyhead;
      // ListNode* ans = dummy->next;
        //delete dummy;
       // return ans;
    }
};

2双指针  指针相差为n 快指针先遍历n   然后同时遍历  遍历到结尾时  慢指针刚好到倒数n的位置 然后删除慢指针所指的位置即可 

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy = new ListNode(0, head);
        ListNode* first = head;
        ListNode* second = dummy;
        for (int i = 0; i < n; ++i) {
            first = first->next;
        }
        while (first) {
            first = first->next;
            second = second->next;
        }
        second->next = second->next->next;
        ListNode* ans = dummy->next;
        delete dummy;
        return ans;
    }
};

链表相交

重点在思路 怎么想到求其差的  差值刚好是长链表

遍历的起点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    ListNode *curA = headA;
    ListNode *curB = headB;
    int lenA =0 ,lenB = 0;
    while(curA != NULL){
        lenA ++;
        curA = curA->next;
    }
    while (curB != NULL) { // 求链表B的长度
         lenB++;
        curB = curB->next;
    }
    curA = headA;
    curB = headB;
    if(lenB>lenA){
        swap(lenA,lenB);
        swap(curA,curB);
    }
    //求长度差
    int gap = lenA-lenB;
   //让curA和curB对齐,再同一起点上(末尾位置对齐)
    while(gap--)
    {
        curA = curA -> next;
    }
    while(curA != NULL)
    {
        if(curA == curB ){
            return curA;
        }
        curA = curA -> next;
        curB = curB -> next; 
    }
    return NULL;


    }
};

142 环形链表

fast先进入环 所以一定会入环   先入环的话就会先走一圈 

Z是快节点比慢节点多走的节点数  

从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点。 慢指针转到入口的时候    快指针转一圈

寻找入口 

while(index1 != index2)

//判断环形入口 相遇之前快指针多走的距离 就是慢指针走的距离

      //即 头节点距离环形入口的距离

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
    ListNode *fast = head;
    ListNode *slow = 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 index2;
      }

    }
    return NULL;
   

    }
};

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值