linked list easy

来源:互联网 发布:社交网络数据可视化 编辑:程序博客网 时间:2024/05/17 03:00

Linked list(easy)

19. Remove Nth Node From End of List

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* removeNthFromEnd(ListNode* head,int n)

   {

       ListNode* fast=head,*slow=head,*pre=head;

       for(int i=0;i<n;i++)

       {

           fast=fast->next;

       }

       while(fast)

       {

           pre=slow;

           slow=slow->next;

           fast=fast->next;

       }

       if(slow==head)

           head=head->next;

       else

           pre->next=slow->next;

        return head;

   }

};

21. Merge Two Sorted Lists

递归

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* mergeTwoLists(ListNode* l1,ListNode* l2)

   {

     ListNode* head=NULL ;

              if (l1 == NULL)

                     return l2;

              if (l2 == NULL)

                     return l1;

              if (l1->val < l2->val)

              {

                  l1->next=mergeTwoLists(l1->next,l2);

                  return l1;

 

              }

              else

              {

                  l2->next=mergeTwoLists(l1,l2->next);

                  return l2;

              }

   }

};

 

非递归

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* mergeTwoLists(ListNode* l1,ListNode* l2)

   {

       if(l1==NULL) return l2;

       if(l2==NULL) return l1;

       ListNode* head=NULL;

       if(l1->val<l2->val)

       {

           head=l1;

           l1=l1->next;

       }

       else

       {

           head=l2;

           l2=l2->next;

       }

        ListNode* p=head;

        while(l1&&l2)

        {

            if(l1->val<l2->val)

            {

                p->next=l1;

                l1=l1->next;

            }

            else

           {

                p->next=l2;

                 l2=l2->next;

            }

            p=p->next;

        }

        if(l1)

            p->next=l1;

        if(l2)

            p->next=l2;

       return head;

   }

};

24. Swap Nodes in Pairs

交换节点非递归版

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* swapPairs(ListNode* head)

   {

      ListNode* dummy=new ListNode(0);

      dummy->next=head;         //因为交换 头指针变化 建立一个头指针之前的空指针

      ListNode* pre=dummy;

      ListNode* cur=head;

      while(cur&&cur->next)

      {

          pre->next=cur->next;

          cur->next=cur->next->next;//先确定后继

          pre->next->next=cur;

       

          pre=cur;

          cur=cur->next;

      }

      return dummy->next;

   }

};

交换节点递归版

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* swapPairs(ListNode* head) {

       if(head==NULL)

           return head;

       if(head->next==NULL)

           return head;

        ListNode *temp=head->next;

        head->next=swapPairs(temp->next);

        temp->next=head;

       return temp;

   }

};

只交换值

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* swapPairs(ListNode* head)

   {

      ListNode* p1=head,* p2;

      int temp;

      while(p1)

      {

          p2=p1->next;

          if(p2)

          {

              temp=p1->val;

              p1->val=p2->val;

              p2->val=temp;

              p1=p1->next;

          }

          p1=p1->next;

      }

      return head;

   }

};

 

83. Remove Duplicates from Sorted List

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* deleteDuplicates(ListNode* head){

       if(head!=NULL)

       {

           ListNode* P=head;

           while(p->next!=NULL)

           {

               if(p->data==p->next->data)

                   p->next=p->next->next;

                else

                   p=p->next;

           }

       }

       return head;

       

       

   }

};

141. Linked List Cycle

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   bool hasCycle(ListNode *head) {

   ListNode *fast=head;

   ListNode *slow=head;

   if(head==NULL)

   {

       return false;

   }

   while(fast->next!=NULL)

   {

       fast=fast->next->next;

       slow=slow->next;

       if(fast==NULL)

       {

           return false;

       }

       if(slow==fast)

       {

           return true;

       }

   }

   return false;

   }

};

160. Intersection of Two Linked Lists

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

  int length(ListNode *head)

       {

           int count=0;

           while(head)

           {

              count++;

              head=head->next;

           }

            return count;

       }

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

       ListNode *p1=headA;

       ListNode *p2=headB;

       int length1=length(headA);

       int length2=length(headB);

       int n=length1-length2;

       for(int i=0;i<abs(n);i++)

       {

           if(n>0)

           p1=p1->next;

           else

           p2=p2->next;

       }

       while(p1&&p2)

       {

           if(p1==p2)

               return p1;

           p1=p1->next;

           p2=p2->next;

       }

       return NULL;

   }

};

 

 

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   int getlength(ListNode *head)

   {

       ListNode *p=head;

       int count=0;

       while(p)

       {

           p=p->next;

           count++;

       }

       return count;

       

   }

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

   {

       ListNode *p1=headA,*p2=headB;

       intn=getlength(headA)-getlength(headB);

       for(int i=0;i<abs(n);i++)

       {

           if(n<0)

               p2=p2->next;

           else

               p1=p1->next;

       }

       while(p1&&p2)

       {

           if(p1==p2)

               return p1;

           p1=p1->next;

           p2=p2->next;

       }

       return NULL;

   }

};

203. Remove Linked List Elements

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* removeElements(ListNode* head,int val)

   {

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

       {

           ListNode *temp = head;

           head=head->next;

           delete temp;

       }

       if(head==NULL)

           return head;

       ListNode* cur=head,* pre=cur;

       cur=cur->next;

       while(cur)

       {

           if(cur->val==val)

           {

               pre->next=cur->next;

               ListNode* temp=cur->next;

               delete cur;

               cur=temp;

           }

           else

           {

               pre=cur;

               cur=cur->next;

           }

       }

       return head;

   }

};

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* removeElements(ListNode* head,int val)

   {

       ListNode* dummy=new ListNode(0);

       dummy->next=head;

       ListNode* pre=dummy;

       ListNode* cur=head;

       while(cur)

       {

           if(cur->val==val)

           {

               pre->next=cur->next;

               ListNode* p=cur->next;

               delete cur;

               cur=p;

           }

           else

           {

               pre=cur;

               cur=cur->next;

           }

       }

       return dummy->next;

   }

};

206. Reverse Linked List

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

/*   ListNode* reverseList(ListNode* head)

   {

       if(!head)

       {

           return NULL;

       }       

       ListNode* p=head;

       ListNode* l=head->next;

       p->next=NULL;

       while(l)

       {

            ListNode* t=l->next;

            l->next=p;

            p=l;

            l=t;

       }

       return p;

   }

*/

  ListNode* reverseList(ListNode* head)

  {

       if(head==NULL||head->next==NULL)

        return head;

       ListNode* p=head;

       head=reverseList(p->next);

       p->next->next=p;

       p->next=NULL;

       return head;

  }

}; 234. Palindrome Linked List

/**

 *Definition for singly-linked list.

 *struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   ListNode* reverse(ListNode* head)

    {

       if(!head)

           return NULL;

       ListNode* p=head;

       ListNode* l=head->next;

       p->next=NULL;

       while(l)

       {

           ListNode* t=l->next;

           l->next=p;

           p=l;

           l=t;

       }

       return p;

    }

   bool isPalindrome(ListNode* head)

    {

       ListNode* slow, *fast;

       slow=fast=head;

       while(fast&&fast->next)

       {

           slow=slow->next;

           fast=fast->next->next;

       }

       if(fast)  //链表元素奇数个

       {

            slow->next=reverse(slow->next);

           slow=slow->next;  //链表元素奇数个,要从slow的下一个位置开始比较

       }

       else     //链表元素偶数个

       {

           slow=reverse(slow);

       }

       

       while(slow)

       {

           if(slow->val!=head->val)

           {

                return false;

           }

                slow=slow->next;

                head=head->next;

       }

       return true;

    }

};

 

237. Delete Node in a Linked List

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *    int val;

 *    ListNode *next;

 *    ListNode(int x) : val(x), next(NULL) {}

 * };

 */

class Solution {

public:

   void deleteNode(ListNode* node) {

       if(node==NULL)

       return;

       node->val=node->next->val;

       node->next=node->next->next;

   }

};

0 0