# Dummy Node，Scenario: When the head is not determinated

ListNode *dummy = new ListNode(0);
dummy->next = head;

1. Remove Duplicates from Sorted List I, II
2. Merge Two Sorted Lists
3. Partition List

## 1. Remove Duplicates from Sorted List I

https://leetcode.com/problems/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.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
}

// 保留第一个，删除后面的
delete tmp;
} else {
}
}

}
};

## 2. Remove Duplicates from Sorted List II

https://leetcode.com/problems/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,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
}

ListNode *dummy = new ListNode(0);

ListNode *pos = dummy;

while (pos->next && pos->next->next) {
if (pos->next->val == pos->next->next->val) {
int preVal = pos->next->val;
// 至少保证有两个node，才会有重复
while (pos->next && pos->next->val == preVal) {
ListNode *tmp = pos->next;
pos->next = tmp->next;
delete tmp;
}
} else {
pos = pos->next;
}
}

return dummy->next;
}
};

## 3. Merge Two Sorted Lists

https://leetcode.com/problems/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.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(0);
ListNode *curr = dummy;

while (l1 && l2) {
if (l1->val < l2->val) {
curr->next = l1;
l1 = l1->next;
} else {
curr->next = l2;
l2 = l2->next;
}

curr = curr->next;
}

if (l1) {
curr->next = l1;
}

if (l2) {
curr->next = l2;
}

return dummy->next;
}
};

## 4. Partition List

https://leetcode.com/problems/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.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
return NULL;
}

ListNode *leftDummy = new ListNode(0);
ListNode *left = leftDummy;

ListNode *rightDummy = new ListNode(0);
ListNode *right = rightDummy;

while (curr) {
if (curr->val < x) {
left->next = curr;
left = left->next;
curr = curr->next;
} else {
right->next = curr;
right = right->next;
curr = curr->next;
}
}

left->next = rightDummy->next;
right->next = NULL;

return leftDummy->next;
}
};

# Basic Skills

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
return NULL;
}

ListNode *pre = NULL;
ListNode *next = NULL;
while (curr) {
next = curr->next;

curr->next = pre;
pre = curr;

curr = next;
}

return pre;
}
};

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
// empty list
// Base case

// reverse from the rest after head
// unlink list from the rest

}
};

## 6. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given m, n satisfy the following condition:
1 ≤ mn ≤ length of list.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
return NULL;
}

ListNode *dummy = new ListNode(0);
ListNode *pos = dummy;

for (int i = 0; i <= m - 2; i++) {
pos = pos->next;
}
ListNode *mPreNode = pos;
ListNode *mCurrNode = pos->next;

ListNode *nPreNode = NULL;
ListNode *nCurrNode = mCurrNode;
ListNode *nNextNode = NULL;
for (int i = m; i <= n; i++) {
nNextNode = nCurrNode->next;

nCurrNode->next = nPreNode;
nPreNode = nCurrNode;

nCurrNode = nNextNode;
}

mPreNode->next = nPreNode;
mCurrNode->next = nCurrNode;

return dummy->next;
}
};

## 7. 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

/**
* 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);

ListNode *pre = dummy;
while (curr) {
if (curr->val == val) {
ListNode *waitForDel = curr;

pre->next = curr->next;
curr = curr->next;

delete waitForDel;
} else {
pre = pre->next;
curr = curr->next;
}
}

return dummy->next;
}
};

## 8. Remove Nth Node From End of List

https://leetcode.com/problems/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,

   Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummy = new ListNode(0);

ListNode *slow = dummy;
ListNode *fast = dummy;
for (int i = 0; i < n - 1; i++) {
fast = fast->next;
}

ListNode *pre = NULL;
while (fast->next) {
pre = slow;
slow = slow->next;
fast = fast->next;
}

pre->next = slow->next;
delete slow;

return dummy->next;
}
};

## 9. Remove Duplicates from Sorted List

https://leetcode.com/problems/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.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
while (pos) {
if (pos->next && pos->val == pos->next->val) {
ListNode *waitForDel = pos->next;
pos->next = pos->next->next;
delete waitForDel;
} else {
pos = pos->next;
}
}

}
};

/**
*
class Solution {
public:
return NULL;
}

while (node->next != NULL) {
if (node->val == node->next->val) {
ListNode *temp = node->next;
node->next = node->next->next;
delete temp;
} else {
node = node->next;
}
}

}
};
*
*/

## 10. Remove Duplicates from Sorted List II

https://leetcode.com/problems/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,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
}

ListNode *dummy = new ListNode(0);

ListNode *pos = dummy;

while (pos->next && pos->next->next) {
if (pos->next->val == pos->next->next->val) {
int preVal = pos->next->val;
// 至少保证有两个node，才会有重复
while (pos->next && pos->next->val == preVal) {
ListNode *tmp = pos->next;
pos->next = tmp->next;
delete tmp;
}
} else {
pos = pos->next;
}
}

return dummy->next;
}
};

（待续）

8

8

8

posted @ 2015-08-17 22:37  Acjx  阅读(1753)  评论(0编辑  收藏  举报