# 前言

## outline：

• Dummy Node in Linked List
• Remove Duplicates from Sorted List II
• Partition List
• Sort List
• Reorder List
• Two Pointers in Linked List (Fast-slow pointers)
• Merge K Sorted Lists

## 课堂笔记

1. Dummy Node in Linked List

Remove Duplicates from Sorted List II

int val = head->val;
}
head = head->next;

    ListNode * deleteDuplicates(ListNode *head) {
}
ListNode dummy = ListNode(0);
}
} else {
}
}
return dummy.next;
}
View Code

    ListNode *reverseBetween(ListNode *head, int m, int n) {
}
ListNode dummy = ListNode(0);
for (int i = 1; i < m; ++i) {
}
// 保存m前一个节点
// 保存第m个节点
// 保存第n个节点
ListNode *nNode = mNode;
// 保存n的后一个节点
ListNode *post = mNode->next;
for (int i = m; i < n; ++i) {
ListNode *tmp = post->next;
post->next = nNode;
nNode = post;
post = tmp;
}
mNode->next = post;
pre->next = nNode;
return dummy.next;
}
View Code

• 当链表的结构发生变化时
• 当需要返回的链表头不确定时

Partition List

    ListNode *partition(ListNode *head, int x) {
}
ListNode dummy1 = ListNode(0);
ListNode dummy2 = ListNode(0);
ListNode *pre = &dummy1;
ListNode *post = &dummy2;
} else {
}
}
// 最后的节点指向空，否则出现死循环
post->next = NULL;
pre->next = dummy2.next;
return dummy1.next;
}
View Code

2. Basic Skills in Linked List

Sort List

    ListNode *merge(ListNode *left, ListNode *right) {
ListNode dummy = ListNode(0);
ListNode *res = &dummy;
while (left && right) {
if (left->val < right->val) {
res->next = left;
left = left->next;
} else {
res->next = right;
right = right->next;
}
res = res->next;
}
if (left) {
res->next = left;
} else {
res->next = right;
}
return dummy.next;
}
}
while(fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
}
ListNode * right = sortList(slow->next);
// 这里非常关键，一定要把左边结尾指向空
slow->next = NULL;
return merge(left,right);
}
View Code

Reorder List

    void merge(ListNode *left, ListNode *right) {
while (left && right) {
ListNode *node1 = left->next;
ListNode *node2 = right->next;
left->next = right;
right->next = node1;
left = node1;
right = node2;
}
}
return;
}
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
}
ListNode *right = slow->next;
slow->next = NULL;
ListNode *ntr = NULL;
while (right && right->next) {
ListNode *tmp = right->next;
right->next = ntr;
ntr = right;
right = tmp;
}
right->next = ntr;
}
View Code

3.fast-slow pointers

Merge K Sorted Lists

    ListNode *mergeTwoLists(ListNode *left, ListNode *right) {
ListNode dummy = ListNode(0);
ListNode *res = &dummy;
while(left && right) {
if (left->val < right->val) {
res->next = left;
left = left->next;
} else {
res->next = right;
right = right->next;
}
res = res->next;
}
if (left) {
res->next = left;
} else {
res->next = right;
}
return dummy.next;
}

ListNode *mergeHelper(vector<ListNode *> &lists, int start, int end) {
if (start == end) {
return lists[start];
}
int mid = start + (end - start) / 2;
ListNode *left = mergeHelper(lists, start, mid);
ListNode *right = mergeHelper(lists, mid + 1, end);
return mergeTwoLists(left,right);
}

ListNode *mergeKLists(vector<ListNode *> &lists) {
if (!lists.size()) {
return NULL;
}
return mergeHelper(lists, 0, lists.size() - 1);
}
View Code

# 总结

1.dummy的使用

2.fast-slow pointer的使用

posted @ 2016-10-17 19:31  RaisingSun_tkw  阅读(2451)  评论(1编辑  收藏  举报