单链表OJ题

 

解题思想是创建两个头结点,分别对应两个单链表的首节点,再建立一个双重循环,判断两个节点是否存在重合,如果重合则相交,返回相交节点

代码如下

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *     int val;

 *     struct ListNode *next;

 * };

 */

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {

    struct ListNode* first=headA;

    struct ListNode* second=headB;

    while(first){

        second=headB;

        while(second){

            if(first==second){

               return first;

            }

            second=second->next;

        }

        first=first->next;

    }

    return NULL;

}

 解释思想是快慢指针,创建两个指针都指向头结点,在一个快指针存在且它的NEXT域存在的循环中,慢指针每次偏移一位,快指针每次偏移两位,判断是否重合,如果重合,则存在环,如果直至循环结束也不重合,则不存在环

代码如下

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *     int val;

 *     struct ListNode *next;

 * };

 */

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(fast==slow){

            return true;

        }

    }

    return false;

}

 解释思想是快慢指针,创建两个指针都指向头结点,在一个快指针存在且它的NEXT域存在的循环中,慢指针每次偏移一位,快指针每次偏移两位,判断是否重合,如果重合,则存在环,如果直至循环结束也不重合,则不存在环,但存在环的时候,在建立一个循环,建立一个新的首节点,令首节点和慢指针每次都偏移一个单位,当两个节点重合的时候,则这个节点就是链表入环的第一个节点

代码如下

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *     int val;

 *     struct ListNode *next;

 * };

 */

typedef struct ListNode Node;

struct ListNode *detectCycle(struct ListNode *head) {

    Node* fast=head;

    Node* slow=head;

    while(fast && fast->next){

        fast=fast->next->next;

        slow=slow->next;

        if(fast==slow){

            Node* first=head;

            while(first != slow){

                first=first->next;

                slow=slow->next;

            }

            return first;

        }

    }

    return NULL;

}

 

解题思想是先建立一个指针指向链表的首元素,在在这个指针存在的循环中,依次申请新节点,将新节点依次放到 原链表的每个元素的后,将原链表的值也拷贝到新的当中,循环结束之后,在建立一个新的节点指向链表的首元素,再将链表的后一个元素拷贝前一个元素的random域,每次循环偏移两个单位,在建立一个循环,断开两个链表即可

代码如下

/**

 * Definition for a Node.

 * struct Node {

 *     int val;

 *     struct Node *next;

 *     struct Node *random;

 * };

 */

typedef struct Node Node;

struct Node* copyRandomList(struct Node* head) {

    if(head==NULL){

        return NULL;

    }

    Node* cur=head;

    while(cur){

        Node* newNode=(Node*)malloc(sizeof(Node));

        newNode->val=cur->val;

        newNode->next=cur->next;

        newNode->random=NULL;

        cur->next=newNode;

        cur=newNode->next;

    }

    cur=head;

    while(cur){

        Node* newNode=cur->next;

        if(cur->random){

           newNode->random=cur->random->next;

        }

        cur=newNode->next;

    }

    cur=head;

    Node* newHead=head->next;

    Node* first=NULL;

    while(cur->next){

        first=cur->next;

        cur->next=first->next;

        cur=first;

    }

    return newHead;

}

解题思想是先建立一个新的节点,新节点的next域指向连连的首元素,建立一个从首元素开始的,元素存在且它的NEXT域存在的循环,如果节点 的next域的值大于这个节点的值,则结束本次循环,如果不大于,则从这个链表的首元素开始遍历,知道寻找到适合他的位置结束,,最后返回新节点的next域

代码如下

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *     int val;

 *     struct ListNode *next;

 * };

 */

 /*

typedef struct ListNode Node;

struct ListNode* insertionSortList(struct ListNode* head){

    while(head){

        Node* newhead=(Node*)malloc(sizeof(Node));

        Node* 

        head=head->next;

    }

}

*/

typedef struct ListNode Node;

struct ListNode* insertionSortList(struct ListNode* head){

    if(head==NULL){

        return NULL;

    }

    Node* newhead=(Node*)malloc(sizeof(Node));

    newhead->next=head;//newhead是首地址

    while(head!=NULL && head->next!=NULL){

        if(head->val<=head->next->val){

            head=head->next;

            continue;

        }

        Node* front=newhead;

        while(head->next->val>=front->next->val){

            front=front->next;

        }

        Node* s=head->next;

        head->next=head->next->next;

        s->next=front->next;

        front->next=s;    

    }

    return newhead->next;

}

代码如下

/**

* struct ListNode {

*	int val;

*	struct ListNode *next;

* };

*

* C语言声明定义全局变量请加上static,防止重复定义

*/

/**

* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可

*

*

* @param pHead ListNode类

* @return ListNode类

*/



typedef struct ListNode Node;

struct ListNode* deleteDuplication(struct ListNode* pHead ) {

// write code here



if(pHead==NULL){

return NULL;

}

Node* prev=NULL;

Node* head=pHead;

Node* head_=pHead->next;

while(head_!=NULL){

if(head->val!=head_->val){

prev=head;

head=head_;

head_=head_->next;

}

else{

if(prev==NULL){

while(head_!=NULL && head_->val==head->val){

head=head_;

head_=head_->next;

}

if(head_!=NULL){

head=head_;

head_=head_->next;

if(head_==NULL){

pHead=head;

return pHead;

}

else{

pHead=head;

}

}else{

return NULL;

}









}

else{

while(head_!=NULL && head_->val==head->val){

head=head_;

head_=head_->next;

}

prev->next=head_;

if(head_==NULL){

return pHead;

}

head=head_;

head_=head_->next;

}



}

}

/*ListNode* newhead=NULL;

while(head){}

while(1){

if(head->val=head->next->val){

head->next=head->next->next;

}

else{



}

}

newhead=head;

head=head->next;

}

*/

return pHead;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值