Thursday, October 29, 2015

Leetcode: 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.
------------------------------------------
-----------------------------------------
Just merge two sorted lists into one sorted list. Nothing special. 
/////////////////////////////////////////////////
//codes
class Solution {
public:
       ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
              //check input
              if (l1 == NULL)return l2;
              if (l2 == NULL)return l1;
              //dummy node
              ListNode*dum = new ListNode(-1), *pl1 = l1, *pl2 = l2, *p = dum;
              while (pl1 != NULL && pl2 != NULL){
                     if (pl1->val<pl2->val){
                           p->next = pl1;
                           p = p->next;
                           pl1 = pl1->next;
                     }
                     else{
                           p->next = pl2;
                           p = p->next;
                           pl2 = pl2->next;
                     }
              }
              //check left nodes and connect to p
              if (pl1 != NULL)p->next = pl1;
              else p->next = pl2;

              p = dum->next;
              delete dum;
              return p;
       }
};

Wednesday, October 28, 2015

Leetcode: Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?

---------------------------------------------
--------------------------------------------
1. find the middle node first, note that
    1   2   3   4 
    1   2   3   4   5
    the middle node for above cases are 2 and 3, respectively. Using two pointers method to find the middle node:
              ListNode *mid = head, *last = head;
              //locate the mid node
              while (last != NULL && last->next != NULL){
                     last = last->next->next;
                     if (last != NULL)mid = mid->next;
              }
2. reverse all the nodes after the middle node. Check here for how to reverse all nodes!
3. check the palindrome with two pointers both in forward direction. 

///////////////////////////////////////////////
//code
class Solution {
public:
       bool isPalindrome(ListNode* head) {
              //check input
              if (head == NULL || head->next == NULL)return true;
              ListNode *mid = head, *last = head;
              //locate the mid node
              while (last != NULL && last->next != NULL){
                     last = last->next->next;
                     if (last != NULL)mid = mid->next;
              }
              //reverse all nodes after mid node
              ListNode *cur = mid->next, *pre = cur->next;
              cur->next = NULL;
              while (pre != NULL){
                     ListNode *tmp = pre->next;
                     pre->next = cur;
                     cur = pre;
                     pre = tmp;
              }
              mid->next = cur;
              //CHECK with two pointers, both forward
              mid = mid->next;
              last = head;
              while (mid != NULL){
                     if (last->val != mid->val)return false;
                     else {
                           last = last->next;
                           mid = mid->next;
                     }
              }
              return true;
       }
};

Leetcode: 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,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

-----------------------------------------
-----------------------------------------
Using two pointers. 
Note this coommand:  while (p->next != NULL && p->next->val<x), the "p->next != NULL" judgement should be put in front of "p->next->val<x". 
///////////////////////////////////////////////////
//code
class Solution {
public:
       ListNode* partition(ListNode* head, int x) {
              //check input
              if (head == NULL)return head;
              //dummy head
              ListNode *dum = new ListNode(-1);
              dum->next = head;
              ListNode  *tmp = head, *p = dum, *f = dum;
              //locate the first node>x
              while (p->next != NULL && p->next->val<x)p = p->next;
              if (p->next == NULL)return head;
              f = p->next;

              //find node <x
              while (f->next != NULL){
                     if (f->next->val<x){
                           tmp = p->next;
                           p->next = f->next;
                           f->next = f->next->next;
                           p->next->next = tmp;

                           p = p->next;
                     }
                     else f = f->next;
              }
              head = dum->next;
              delete dum;
              return head;

       }
};

Tuesday, October 27, 2015

Leetcode:delete all duplicate nodes

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
---------------------------------------------------

1. Using dummy node as the head may be deleted.
2. note the following two cases:

  •      define a int val to guarantee all duplicates are deleted. 
  •      define a pointer p to cover two nodes, note the boundary check



///////////////////////////////////////
//code
class Solution {
public:
       ListNode* deleteDuplicates(ListNode* head) {
              //check input
              if (head == NULL)return head;
              ListNode *dum = new ListNode(-1), *tmp = head, *p = head;
              dum->next = head;
              p = dum;
              int val = INT_MAX;
              while (p->next->next != NULL){
                     if (p->next->val == p->next->next->val || p->next->val == val){
                           val = p->next->val;
                           tmp = p->next;
                           p->next = tmp->next;
                           delete tmp;
                     }
                     else p = p->next;
              }
              //delete left val
              if (p->next->val == val){
                     tmp = p->next;
                     p->next = tmp->next;
                     delete tmp;
              }
              head = dum->next;
              delete dum;
              return head;
       }

};

Leetcode: 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.
-------------------------------------------------
This problem doesn't need a dummy in head, as the first one can be functioned as a dummy due to that the first node is always kept in the process. 
//////////////////////////////////////////////
//code
class Solution {
public:
       ListNode* deleteDuplicates(ListNode* head) {
              //check input
              if (head == NULL)return head;
              ListNode *tmp = head, *p = head;
              while (p->next != NULL){
                     if (p->next->val == p->val){
                           tmp = p->next;
                           p->next = tmp->next;
                           delete tmp;
                     }
                     else p = p->next;
              }
              return head;
       }
};


Leetcode: Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
-------------------------------------------------
Using a dummy node in head to simplify the procedure!
////////////////////////////////////
//code
class Solution {
public:
       ListNode* removeElements(ListNode* head, int val) {
              //check input
              //if(head==NULL)return head;
              ListNode *p = head, *tmp = head, *dum = new ListNode(-1);
              dum->next = head;
              p = dum;
              while (p->next != NULL){
                     if (p->next->val == val){
                           tmp = p->next;
                           p->next = tmp->next;
                           delete tmp;
                     }
                     else p = p->next;
              }
              head = dum->next;
              delete dum;
              return head;
       }
};