leetcode链表题

1. sort_list

Sort a linked list in O(n log n) time using constant space complexity.

分析:时间复杂度是nlogn,所以可以考虑归并排序。取中点,对左边和右边分别递归排序,最后合并。

知识点:快慢指针,用来取链表中点;归并排序。

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *sortList(ListNode *head) {
12         if(!head||!head->next)return head;
13         ListNode *first = head;
14         ListNode *second = head->next;
15         while(second&&second->next){
16             first=first->next;
17             second=second->next->next;
18         }
19         ListNode *left = sortList(first->next);
20         first->next = NULL;
21         ListNode *right = sortList(head);
22         return merge(left,right);
23     }
24     ListNode *merge(ListNode *head1, ListNode *head2)
25      {
26          if(head1 == NULL)return head2;
27          if(head2 == NULL)return head1;
28          ListNode *res , *p ;
29          if(head1->val < head2->val)
30              {res = head1; head1 = head1->next;}
31          else{res = head2; head2 = head2->next;}
32          p = res;
33          
34          while(head1 != NULL && head2 != NULL)
35          {
36              if(head1->val < head2->val)
37              {
38                  p->next = head1;
39                  head1 = head1->next;
40              }
41              else
42              {
43                  p->next = head2;
44                  head2 = head2->next;
45              }
46              p = p->next;
47          }
48          if(head1 != NULL)p->next = head1;
49          else if(head2 != NULL)p->next = head2;
50          return res;
51      }
52 };

2. linked-list-cycle-ii

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

Follow up:
Can you solve it without using extra space?

分析:寻找链表的入环节点,也是使用快慢指针,快慢指针第一次相遇后将快指针放回链表头,然后以相同的速度前进,再次相遇的节点就是链表的入环节点。

知识点:快慢指针找入环节点

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *detectCycle(ListNode *head) {
12         while(!head||!head->next)return nullptr;
13         ListNode *slow = head;
14         ListNode *fast = head;
15         while(fast != NULL && fast->next != NULL){
16             slow=slow->next;
17             fast=fast->next->next;
18             if(fast==slow)break;
19         }
20         if(!fast||!fast->next)return nullptr;
21         fast=head;
22         while(slow != fast){
23             slow = slow->next;
24             fast = fast->next;
25         }
26         return slow;
27     }
28 };

 3. convert-sorted-list-to-binary-search-tree

     Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

分析:可以递归建立平衡二叉查找树,将链表中间的值设为树的根,对左边和右边分别建立二叉查找树。

知识点:递归,快慢指针

class Solution {
public:
    TreeNode *sortedListToBST(ListNode *head) {
        return ToBST(head,nullptr);
    }
    TreeNode *ToBST(ListNode *head,ListNode *tail){
        if(head==tail)return nullptr;
        ListNode *fast = head;
        ListNode *slow = head;
        while(fast!=tail&&fast->next!=tail){
            fast=fast->next->next;
            slow=slow->next;
        }
        TreeNode *tr= new TreeNode(slow->val);
        tr->left = ToBST(head,slow);
        tr->right = ToBST(slow->next,tail);
        return tr;
    }
};

 4. partition-list

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given1->4->3->2->5->2and x = 3,
return1->2->2->4->3->5.

 分析:将小于某值的节点按顺序移至前面,我这里为了方便直接建立了两个vector分别按序存储值小于x和大于等于x的节点。

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        vector<int> a;
        vector<int> b;
        ListNode *li=head;
        while(li){
            if(li->val<x)a.push_back(li->val);
            else b.push_back(li->val);
            li = li->next;
        }
        li = head;
        for(int i=0;i<a.size();i++){
            li->val=a[i];
            li=li->next;
        }
        for(int i=0;i<b.size();i++){
            li->val=b[i];
            li=li->next;
        }
        return head;
    }
};

5. remove-duplicates-from-sorted-list

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

For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3. 

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        ListNode *li = head;
        while(li&&li->next){
            while(li&&li->next&&(li->next->val == li->val))
                li->next = li->next->next;
            li = li->next;
        }
        return head;
    }
};

 6. remove-duplicates-from-sorted-list-ii

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.

分析:这里创建了一个新的头节点,作为第一个不重复的节点;

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if((!head)||(!head->next))return head;
        ListNode *newHead = new ListNode(head->val-1);
        newHead->next = head;
        ListNode *last = newHead;
        ListNode *cur = head;
        while(cur&&cur->next){
            if(cur->val!=cur->next->val){
                last = cur;
            }
            else{
                while(cur&&cur->next&&(cur->val==cur->next->val)){
                    cur = cur->next;
                }
                last->next = cur->next;
            }
            cur = cur->next;
        }
        return newHead->next;
    }
};

 

posted on 2018-03-08 21:08  Apiu  阅读(539)  评论(0编辑  收藏  举报

导航