[C语言][LeetCode][86]Partition List
来源:互联网 发布:云计算的未来发展趋势 编辑:程序博客网 时间:2024/06/06 11:40
题目
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.
标签
Linked List、Two Pointers
难度
中等
分析
题目意思是给定一个value,将单链表里比value小的放在左侧,大于或等于value的放到右侧,最后返回链表。我的做法是比value小的拼成一个链表,其他的拼成另一个链表,再将两个链表连接起来。
C代码实现
struct ListNode* partition(struct ListNode* head, int x) { struct ListNode *leftHead, *rightHead, *left, *right; struct ListNode *ret; if(!head) return NULL; leftHead = (struct ListNode *)malloc(sizeof(struct ListNode)); rightHead = (struct ListNode *)malloc(sizeof(struct ListNode)); left = leftHead; right = rightHead; while(head) { if(head->val < x) { left->next = head; left = left->next; } else { right->next = head; right = right->next; } head = head->next; } right->next = NULL; left->next = rightHead->next; ret = leftHead->next; if(leftHead) free(leftHead); if(rightHead) free(rightHead); return ret;}
0 0
- [C语言][LeetCode][86]Partition List
- [leetcode-86]Partition List(c)
- 【LeetCode】 86. Partition List C语言
- LeetCode 86: Partition List
- LeetCode 86 Partition List
- [leetcode 86] Partition List
- leetcode-86 Partition List
- [LeetCode 86]Partition List
- leetcode || 86、Partition List
- LeetCode---(86) Partition List
- LeetCode 86 Partition List
- Leetcode[86]-Partition List
- leetcode[86]:Partition List
- Leetcode #86 Partition List
- LeetCode(86) Partition List
- leetcode 86: Partition List
- [Leetcode]#86 Partition List
- leetCode #86 Partition List
- WIN7下使用Xmanager连接rhel-server-6.2-i386配置
- [SSM]使用MyBatis Generator自动创建代码
- 文本特征选择算法:卡方检验和信息增益
- springmvc jsp无法访问静态同目录的css js文件
- 堆排序
- [C语言][LeetCode][86]Partition List
- 自定义控件开关,绘制图片。
- Pku1200 Crazy Search(Rabin Karp)
- leetcode——largestRectangleArea
- 字符串的翻转问题
- REST 在Servlet容器中运行
- POJ 2817 WordStack
- arcgis_标注功能
- 程序员你为什么迷茫?