编程题-分隔链表
发布时间
阅读量:
阅读量
/** * Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* partition(struct ListNode* head, int x) {
if(head == NULL || head->next == NULL)//如果链表是空的或者只有一个结点,直接返回
return head;
struct ListNode* head1 = NULL;//新链表的头指针
struct ListNode* cur = head; //用来遍历原链表
struct ListNode* cur1 = NULL; //指向新链表,用于往新链表中插入新结点,因为不能打乱原来的顺序,所以要尾插
struct ListNode* prev = NULL; //用于连接原链表中所有小于x的结点
while(cur != NULL)
{
if(cur->val < x)//小于x的结点就让它保持不动,还在原链表中
{
全部评论 (0)
还没有任何评论哟~
