Leetcode解题笔记(Linked List)

来源:互联网 发布:海康威视管理端口设置 编辑:程序博客网 时间:2024/06/03 09:14

2016-07-16更新:

19.Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.

For example,
这里写图片描述
Note:
Given n will always be valid.
Try to do this in one pass.

题目大意是删除单链表中倒数第n个节点,经典的双指针法。让快指针前进n个单位后慢指针开始移动,当快指针到达表尾时慢指针停在倒数第n个位置上。
代码如下:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {    struct ListNode *fast=head,*slow=head ,*tmp=NULL;    int i=0;    if(!head||!n)        return head;    if(!head->next)/*如果只有一个节点,则返回空链表*/        return NULL;    while(n){        fast=fast->next;        n--;        if(!fast)/*如果快指针没有前进n个单位就已经为空,则返回head->next,也就是去除了第一个元素,很简单,自行推导*/            return head->next;    }    /*找到待删除结点的前驱*/    while(fast->next){        slow=slow->next;        fast=fast->next;    }    tmp=slow->next;    slow->next=tmp->next;    free(tmp);    tmp=NULL;    return head;}

21.Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
题目大意是合并两个有序单链表,思路就是声明head作为新链表的头指针,指针p在新链表上移动。比较l1与l2大小关系决定新的头指针的指向,之后继续比较两链表后序元素的大小关系,将较小的插入进新链表即可。代码如下 :

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {    struct ListNode * head=NULL;    if(!l1)        return l2;    if(!l2)        return l1;    if(!l1&&!l2)        return NULL;    if(l1->val>l2->val){        head=l2;        l2=l2->next;    }    else{        head=l1;        l1=l1->next;    }    struct ListNode * p=head;    while(l1&&l2){        if(l1->val>l2->val){            p->next=l2;            l2=l2->next;        }        else{            p->next=l1;            l1=l1->next;        }        p=p->next;    }    if(l1)/*复制剩余元素*/        p->next=l1;    if(l2)        p->next=l2;    return head;}

24.Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

题目大意:成对交换链表中的节点
思路:这道题参考了讨论区,发现有人巧妙地利用递归解决问题,代码如下:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* swapPairs(struct ListNode* head) {    if(!head||!head->next)        return head;    struct ListNode * tmp=head->next;    head->next=swapPairs(tmp->next);/*使链表中第二对及之后的节点进行交换*/    tmp->next=head;    return tmp;}

83.Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

题目大意是删除单链表中的重复元素,很简单的双指针法的应用,注意循环边界条件就可以了。代码如下:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* deleteDuplicates(struct ListNode* head) {    if(!head||!head->next)        return head;    struct ListNode * pmove=head;    struct ListNode * qmove=NULL;    struct ListNode * tmp=NULL;    while(pmove){        qmove=pmove;        while(qmove->next){            if(qmove->next->val==pmove->val){                tmp=qmove->next;                qmove->next=tmp->next;                free(tmp);            }            else                qmove=qmove->next;        }        pmove=pmove->next;    }    return head;}

141.Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?
题目大意:判断链表是否有环,能否不用额外空间完成算法?
思路:典型的快慢指针问题,只需让快指针速度为慢指针二倍,判断快慢指针是否会相等即可,不过要注意循环条件。代码如下:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */bool hasCycle(struct ListNode *head) {    struct ListNode * pmove=head;    struct ListNode * qmove=head;    if(!head)        return false;    while(pmove&&qmove&&qmove->next){        pmove=pmove->next;        qmove=qmove->next->next;        if(pmove==qmove)            return true;    }    return false;}
0 0
原创粉丝点击