Leetcode刷题——单链表2

简介: 笔记

练习题1 链表分割


1.png

思路:

2.png



将链表一分为2,以x为界限,大于x的尾插到新链表1,小于x的尾插到新链表2


,之后再将新链表1,头插到新链表2,跟归并排序有点像


class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
          ListNode*Guard1=(ListNode*)malloc(sizeof(ListNode));
         ListNode*Guard2=(ListNode*)malloc(sizeof(ListNode));
         ListNode*tail1=Guard1;
         ListNode*tail2=Guard2;
        while(pHead)
        {
            if(pHead->val<x)
            {
                tail1->next=pHead;
             tail1=tail1->next;
            }
            else
            {
                tail2->next=pHead;
                tail2=tail2->next;
            }
                 pHead=pHead->next;
        }
        tail1->next=Guard2->next;
        tail2->next=NULL;
        free(Guard2);
        return Guard1->next;
    }
};


练习题2 链表的回文结构


思路:


1.先找到中间节点

3.png

2.从中间节点一直到末尾节点内的节点进行逆置

4.png



3. 用俩个节点分别指向头和中间位置,然后挨个进行数字比较,若不想等,直接退出


5.png


注:当奇数时候,第一个2,仍然指向3,因此判断相等


6.png

struct ListNode* middleNode(struct ListNode* head){
struct ListNode*fast,*slow;
fast=slow=head;
while(fast&&fast->next)
{
    fast=fast->next->next;
    slow=slow->next;
}
return slow;
}struct ListNode* reverseList(struct ListNode* head){
    struct ListNode*cur=head;
    struct ListNode*prve=NULL;
    while(cur)
    {
        struct ListNode*t=cur->next;
        cur->next=prve;
        prve=cur;
        cur=t;
    }
    return prve;
}
class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) {
        ListNode*mid=middleNode(A);
        struct ListNode*rmid=reverseList(mid);
        while(A&&rmid)
        {
            if(A->val!=rmid->val)
                return false;
            else
            {
                rmid=rmid->next;
                A=A->next;
            }
        }
        return true;
    }
};


方法二:临时拷贝一份,然后一个一个比较


练习题3 链表相交


160. 相交链表 - 力扣(LeetCode)

7.png

相交链表特点:最后一个节点的地址相等


方法:1.求俩个出链表的长度,即让俩个链表都走到尾节点


           2.求出他们的距离差d


           3.较长的链表先走d步,然后俩个链表开始一起走,每次走一步


struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *newhead1=headA;
  struct ListNode *newhead2=headB;
  int k=1;int z=1;int c=0;
  while(newhead1->next)
  {
      newhead1=newhead1->next;
      k++;
  }
  while(newhead2->next)
  {
      newhead2=newhead2->next;
      z++;
  }
  if(newhead1!=newhead2)
  return NULL;
c=abs(k-z);
while(c--)
{
    if(k>z)
   headA=headA->next;
    else
    headB=headB->next;
}
while(headA&&headB)
{
    if(headA==headB)
    return headA;
    else
    {
        headA=headA->next;
        headB=headB->next;
    }
}
return NULL;
}


struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *newhead1=headA;
  struct ListNode *newhead2=headB;
  int k=1;int z=1;
  while(newhead1->next)
  {
      newhead1=newhead1->next;
      k++;
  }
  while(newhead2->next)
  {
      newhead2=newhead2->next;
      z++;
  }
  if(newhead1!=newhead2)
  return NULL;
int gap=abs(k-z);
 struct ListNode *Longlist=headA;
  struct ListNode *ShoreList=headB;
if(k<z)
{
    Longlist=headB;
    ShoreList=headA;
}
while(gap--)
{
    Longlist=Longlist->next;
}
while(Longlist!=ShoreList)
{
      Longlist=Longlist->next;
      ShoreList=ShoreList->next;
}
return Longlist;
}


练习题4 判断是否为环形链表


141. 环形链表 - 力扣(LeetCode)

8.png

带环链表:1.不能遍历,会陷入死循环


利用快慢指针,快指针一次走俩步,慢指针走一步

9.png



当slow走到中间位置时,fast进入环内

10.png

当慢指针进环时,快指针在环内已经走了一小会了,具体走到了哪里,无法知晓 (要根据环的大小决定),但是可以知道fast在环里走的路程,是slow从中间到进环路程的2倍


slow进入环后,看作是fast追赶slow

11.png



假设在红色位置fast追上了slow


bool hasCycle(struct ListNode *head) {
     struct ListNode *fast=head;
     struct ListNode *slow=head;
     while(fast&&fast->next)
     {
         fast=fast->next->next;
         slow=slow->next;
         if(slow==fast)
         return true;
     }
     return false;
}

快慢指针延伸问题

12.png13.png

证明:slow走一步,fast走俩步,fast能追上slow


假设:slow进环以后,fast和slow之间的差距是N,即追赶距离为N

14.png

slow和fast每移动一次,他们的距离会缩小一格


因此距离由:N,N-1,N-2,N-3……0


所以,能追上


证明:slow走一步,fast走三部,能否追得上


假设:slow进环以后,fast和slow之间的差距是N,即追赶距离为N

15.png

一开始fast在3,slow进入圆环


slow和fast每移动一次,他们的距离会缩小2格,他们的距离N=9

16.png17.png

此时距离是-1,-1意味着,他们之间的距离变成了C-1(C是环的长度)


最终距离是0还是其他数字,由N决定


如果N是奇数,N=9


9,7,5,3,1,-1


-1意味着,他们之间的距离变成了C-1(C是环的长度)


如果C-1是偶数,即他们的距离是偶数每次-2,一定追得上,如果C-1是奇数,又得去判断下一次C-1是偶数还是奇数


因此,能否追的上如果距离是偶数,则追得上


     如果距离是奇数,得看C-1是否为偶


练习题5 找环形链表的节点


142. 环形链表 II - 力扣(LeetCode)

18.png

方式1.公式证明


用快慢指针:fast走的距离=2*slow的距离


公式推导


假设进环前的长度L,假设环的长度C,入口点到相遇点距离x

19.png

slow所走距离:L+X,慢指针所走的距离不可能超过一圈,因为N最大是C-1


fast所走距离:L+NC+X,N代表fast走过的圈数,N>=1


2(L+X)=L+X+NC


(L+X)=NC


L=NC-X


L=(N-1)*C+C-X


左边是A所走距离,右边是B所走距离


可证明:一个指针A从头开始走,另一个指针B从相遇点开始走,最终会在入口点相遇


struct ListNode* detectCycle(struct ListNode* head) {
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    struct ListNode* meetnextl =NULL;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (fast == slow)
        {
            meetnextl = fast;
         while(head!=meetnextl)
         {
             meetnextl=meetnextl->next;
             head=head->next;
         }
         return head;
        }
    }
    return NULL;
}


方法2:转换成相交问题

20.png

fast旁白的黑色是相遇点,下面蓝色是相遇点的下一个点,A和B链表的交点,就是入口点

然后让长的先走,接着同时走,跟上面的题一样


struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *newhead1=headA;
  struct ListNode *newhead2=headB;
  int k=1;int z=1;
  while(newhead1->next)
  {
      newhead1=newhead1->next;
      k++;
  }
  while(newhead2->next)
  {
      newhead2=newhead2->next;
      z++;
  }
  if(newhead1!=newhead2)
  return NULL;
int gap=abs(k-z);
 struct ListNode *Longlist=headA;
  struct ListNode *ShoreList=headB;
if(k<z)
{
    Longlist=headB;
    ShoreList=headA;
}
while(gap--)
{
    Longlist=Longlist->next;
}
while(Longlist!=ShoreList)
{
      Longlist=Longlist->next;
      ShoreList=ShoreList->next;
}
return Longlist;
}
struct ListNode *hasCycle(struct ListNode *head) {
     struct ListNode *fast=head;
     struct ListNode *slow=head;
     while(fast&&fast->next)
     {
         fast=fast->next->next;
         slow=slow->next;
         if(slow==fast)
         return slow;
     }
     return fast;
}
struct ListNode* detectCycle(struct ListNode* head) {
    struct ListNode* guard1 = hasCycle(head);
    if(guard1==NULL)
    return NULL;
     struct ListNode*fast=head;
      struct ListNode*slow=head;
    struct ListNode* meetnextl =NULL;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
      if (fast == slow)
        {
            meetnextl = fast->next;
            fast->next=NULL;
             struct ListNode* guard2 = getIntersectionNode(head, meetnextl);
             return guard2;
        }
    }
    return NULL;
}


练习题6 复制带随机指针的链表


138. 复制带随机指针的链表 - 力扣(LeetCode)

21.png

思路:

22.png

1.先拷贝各个节点,插到相对应的节点后面,然后将链表连接起来


2.然后将cur的random所指指针赋值给copy的random


if(cur->random!=NULL)


copy->random=cur->random->next


3.将各节点拷贝下来进行链接,顺便链接原链表

23.png



* struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */
struct Node* copyRandomList(struct Node* head) {
    struct Node*cur=head;
      struct Node* next=NULL;
          struct Node*copy=NULL;
    while(cur)
    {
        //1.拷贝一份插入链表内
       copy=(struct Node*)malloc(sizeof( struct Node));
      next=cur->next;
      copy->val=cur->val;
        cur->next=copy;
        copy->next=next;
        cur=next;
    }
    cur=head;
  while(cur)
    {   //2.链接random
    copy=cur->next;
    if(cur->random==NULL)
   copy->random=NULL;
   else
        copy->random=cur->random->next;
        cur=cur->next->next;
    }
    cur=head;
    struct Node*newhead=NULL;
    struct Node*tail=NULL;
    while(cur)
    {
        copy=cur->next;
        next=copy->next;
        if(newhead==NULL)
        newhead=tail=copy;
        else
        {
            tail->next=copy;
            tail=tail->next;
        }
        cur->next=next;
        cur=next;
    }
    return newhead;
}


相关文章
|
2月前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
2月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
2月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3
|
2月前
|
容器
《LeetCode》——LeetCode刷题日记1
《LeetCode》——LeetCode刷题日记1
|
2月前
|
算法
LeetCode刷题---21.合并两个有序链表(双指针)
LeetCode刷题---21.合并两个有序链表(双指针)
|
2月前
|
算法 测试技术
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
|
2月前
|
存储
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
144 38
|
8天前
刷题之Leetcode160题(超级详细)
刷题之Leetcode160题(超级详细)
11 0
|
8天前
刷题之Leetcode206题(超级详细)
刷题之Leetcode206题(超级详细)
21 0
刷题之Leetcode206题(超级详细)
|
8天前
|
索引
刷题之Leetcode707题(超级详细)
刷题之Leetcode707题(超级详细)
13 0

热门文章

最新文章