careercup-链表 2.4

2.4 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前。

思路:将小于的结点还是保存在原来的链表中,将大于等于x的结点加入一个新的链表,最后将这两个链表链接起来。

C++实现代码:

#include<iostream>
#include<new>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL) {}
};

void createList(ListNode *&L)
{
    int arr[10]= {1,2,3,2,5,6,7,3,9,1};
    int i;
    ListNode *p=NULL;
    for(i=0; i<10; i++)
    {
        ListNode *tmp=new ListNode(arr[i]);
        if(L==NULL)
        {
            L=tmp;
            p=tmp;
        }
        else
        {
            p->next=tmp;
            p=tmp;
        }
    }
}

void splitX(ListNode *L,int x)
{
    if(L==NULL)
        return;
    ListNode *pre=L;
    ListNode *p=L;
    ListNode *L1=NULL;
    ListNode *q=NULL;
    while(p)
    {
        if(p->val<x)
        {
            pre=p;
            p=p->next;
        }
        else
        {
            ListNode *tmp=p;
            p=tmp->next;
            pre->next=p;
            tmp->next=NULL;
            if(L1==NULL)
            {
                L1=tmp;
                q=tmp;
            }
            else
            {
                q->next=tmp;
                q=tmp;
            }
        }
    }
    pre->next=L1;
}

int main()
{
    ListNode *head=NULL;
    createList(head);
    ListNode *p=head;
    while(p)
    {
        cout<<p->val<<" ";
        p=p->next;
    }
    cout<<endl;
    splitX(head,5);
    p=head;
    while(p)
    {
        cout<<p->val<<" ";
        p=p->next;
    }
    cout<<endl;
}
时间: 2024-10-03 22:54:36

careercup-链表 2.4的相关文章

CareerCup之2.2 寻找单链表倒数第n个元素

[题目] 原文: 2.2 Implement an algorithm to find the nth to last element of a singly linked list. 译文: 实现一个算法从一个单链表中返回倒数第n个元素. [分析] (1)创建两个指针p1和p2,指向单链表的开始节点. (2)使p2移动n-1个位置,使之指向从头开始的第n个节点.(意思是就是使p1和p2距离n个位置) (3)接下来检查p2 - > = = null 如果yes返回p1的值,否则继续移动p1和 p

CareerCup之2.1无序链表删除重复元素

[题目] 原文: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? 译文: 从一个未排序的链表中移除重复的项 进一步地, 如果不允许使用临时的缓存,你如何解决这个问题? [分析] (1)如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况.

[CareerCup] 2.7 Palindrome Linked List 回文链表

2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome Linked List 回文链表.

[CareerCup] 2.6 Linked List Cycle 单链表中的环

2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the

[CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed? 这道题让我们移除无序链表中的重复项,在LeetCode中有两道类似的题是Remove Duplicates from Sorted List 移除有序链表中的重复项 和 Remove Duplicates fr

[CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元素,LeetCode中相类似的题目有Kth Largest Element in an Array 数组中第k大的数字 和 Kth Smallest Element in a BST 二叉搜索树中的第K小的元素.但那两道题和这题又不一样,首先这道题是要在链表中操作,链表的特点就是不能通过下标来直接访

[CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >

[CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表

4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists).

[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to merge two binary search tree into balanced binary search tree.. Let there be m elements in first tree and n elements in the other tree. Your merge funct