JZ35 (h)复杂链表的复制
- 链表拼接、拆分
先将每个节点拷贝一份,然后再逐步处理随机指针,最后将原链表和拷贝链表拆分开
注意最后要将两个链表完全拆分,不然无法通过测试
JZ76 (h)删除链表中重复的结点
- 遍历
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
ListNode *vhead = new ListNode(-1);
vhead->next = pHead;
ListNode *pre = vhead, *cur = pHead;
while (cur) {
if (cur->next && cur->val == cur->next->val) {
cur = cur->next;
while (cur->next && cur->val == cur->next->val) {
cur = cur->next;
}
cur = cur->next;
pre->next = cur;
}
else {
pre = cur;
cur = cur->next;
}
}
return vhead->next;
}
};