ARTS-15--链表里面按指定数目进行拆分

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Danny_idea/article/details/99494989

概述:
左耳朵耗子专栏《左耳听风》 用户自发每周完成一个ARTS:

1.Algorithm:每周至少做一个 leetcode 的算法题

2.Review:阅读并点评至少一篇英文技术文章

3.Tip:学习至少一个技术技巧

4.Share:分享一篇有观点和思考的技术文章

Algorithm
题目概述:

给出一个链表和一个值x,以x为参照将链表划分成两部分,使所有小于x的节点都位于大于或等于x的节点之前。
两个部分之内的节点之间要保持的原始相对顺序。
例如:
给出1->4->3->2->5->2和x = 3,
返回1->2->2->4->3->5

代码思路



public class LinkedListSortDemo {

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode partition(ListNode head, int x) {
        if(head==null)
            return null;
        ListNode dummy1=new ListNode(0);
        ListNode dummy2=new ListNode(0);
        ListNode curr1=dummy1;
        ListNode curr2=dummy2;
        while(head!=null){
            if(head.val<x){
                curr1.next=head;
                curr1=curr1.next;
            }else{
                curr2.next=head;
                curr2=curr2.next;
            }
            head=head.next;
        }
        curr2.next=null;
        curr1.next=dummy2.next;
        return dummy1.next;
    }

    public static void main(String[] args) {
        LinkedListSortDemo l = new LinkedListSortDemo();
        ListNode listNode=new ListNode(1);
        ListNode temp=listNode;
        temp.next=new ListNode(4);
        temp=temp.next;

        temp.next=new ListNode(3);
        temp=temp.next;

        temp.next=new ListNode(2);
        temp=temp.next;

        temp.next=new ListNode(5);
        temp=temp.next;

        temp.next=new ListNode(2);
        temp=temp.next;

        ListNode result = l.partition(listNode,3);
        System.out.println("---");

    }

}

 

 

展开阅读全文

divide()函数(按奇偶拆分链表)错在哪啊?

09-19

// chain.cpp : 定义控制台应用程序的入口点。rn//rnrn#include "stdafx.h"rn#include rnusing namespace std;rnrntemplate class Chain ;rntemplate class ChainIterator ;rntemplate rnclass ChainNodernrnpublic:rn friend ChainIterator;rn friend Chain;rn T data;rn ChainNode *link;rn;rnrntemplaternclass Chain rnrn friend ChainIterator;rnpublic:rn Chain()rn first = 0;rn ChainNode * Last()rn rn ChainNode *cur=first;rn while(cur->link)rn rn cur=cur->link;rn rn return cur;rn rn ChainNode * Pre(ChainNode *q)rn rn if(q==first)rn return 0;rn elsern rn ChainNode *cur=first;rn //ifq==first)rn //return 0;rn while((cur->link->data)!=(q->data))rn rn cur=cur->link;rn rn return cur;rn rn rn ~Chain();rnrn bool IsEmpty() const return first == 0;rn int Length() const; rn bool Find(int k, T& x) const; rn int Search(const T& x) const; rn Chain& Delete(int k, T& x); rn Chain& Insert(int k, const T& x);rn void Output(ostream& out) const;rn //Chain& merge(Chain &A,Chain &B);rn //Chain & divide(Chain &C);rn //void print(Chain &C);rn //private:rn ChainNode *first; // pointer to first nodern;rntemplaternChain::~Chain()rn// Chain destructor. Delete all nodes in chain.rn ChainNode *next; // next nodern while (first) rn next = first->link;rn delete first;rn first = next;rn rnrnrntemplaternint Chain::Length() constrn// Return the number of elements in the chain.rn ChainNode *current = first;rn int len = 0;rn while (current) rn len++;rn current = current->link;rn rn return len;rnrnrntemplaternbool Chain::Find(int k, T& x) constrn// Set x to the k'th element in the chain.rn // Return false if no k'th; return true otherwise.rn if (k < 1) return false;rn ChainNode *current = first;rn int index = 1; // index of currentrn while (index < k && current) rn current = current->link;rn index++;rn rn if (current) x = current->data;rn return true;rn return false; // no k'th elementrnrnrntemplaternint Chain::Search(const T& x) constrn// Locate x. Return position of x if found.rn // Return 0 if x not in the chain.rn ChainNode *current = first;rn int index = 1; // index of currentrn while (current && current->data != x) rn current = current->link;rn index++;rn rn if (current) return index;rn return 0;rnrnrntemplaternChain& Chain::Delete(int k, T& x)rn// Set x to the k'th element and delete it.rn // Throw OutOfBounds exception if no k'th element.rn if (k < 1 || !first)rn throw OutOfBounds(); // no k'thrnrn // p will eventually point to k'th nodern ChainNode *p = first;rnrn // move p to k'th & remove from chainrn if (k == 1) // p already at k'thrn first = first->link; // removern else // use q to get to k-1'strn ChainNode *q = first;rn for (int index = 1; index < k - 1 && q;rn index++)rn q = q->link;rn if (!q || !q->link)rn throw OutOfBounds(); // no k'thrn p = q->link; // k'thrn q->link = p->link; // remove from chainrnrn // save k'th element and free node prn x = p->data;rn delete p;rn return *this;rnrnrntemplaternChain& Chain::Insert(int k, const T& x)rn// Insert x after the k'th element.rn // Throw OutOfBounds exception if no k'th element.rn // Pass NoMem exception if inadequate space.rnrnrn //modefy by mern //if (k < 0) throw OutOfBounds();rnrn // p will eventually point to k'th nodern ChainNode *p = first;rn for(int index = 1; index < k && p;rn index++) // move p to k'thrn p = p->link;rn // if (k > 0 && !p) throw OutOfBounds(); // no k'thrnrn // insertrn ChainNode *y = new ChainNode;rn y->data = x;rn if (k) // insert after prn y->link = p->link;rn p->link = y;rn else // insert as first elementrn y->link = first;rn first = y;rn return *this;rnrnrntemplaternvoid Chain::Output(ostream& out) constrn// Insert the chain elements into the stream out.rn ChainNode *current;rn for (current = first; current;rn current = current->link)rn out << current->data << " ";rnrnrn// overload <rnostream& operator<<(ostream& out, const Chain& x)rnrn x.Output(out); return out;rnrntemplaternChain& merge(Chain &A,Chain &B)rnrn if(!A.first)rn return B;rn if(!B.first)rn return A;rn ChainNode *p,*q,*pre;rn p=A.first;q=B.first;rn int a,b;rn //T x;rn a=A.Length();rn b=B.Length();rn if(a<=b)rn rn for(;p;p=A.first)rn rn while(q&& ((p->data)>(q->data)))rn rn q=q->link;rn rn if(!q)rn rn q=B.Last();rn q->link=p;rn return B;rn rn elsern rn A.first=p->link;rn //temp=p;rn pre=B.Pre(q);rn if(pre)rn rn //temp->link=q;rn p->link=q;rn pre->link=p;rn rn elsern rn p->link=q;rn B.first=p;rn rn rnrn rn return B;rn rn else if(a>b)rn rn for(;q;q=B.first)rn rn while(p&& ((p->data)<(q->data)))rn rn p=p->link;rn rn if(!p)rn rn p=A.Last();rn p->link=q;rn return A;rn rn elsern rn B.first=q->link;rn //temp=p;rn pre=A.Pre(p);rn if(pre)rn rn //temp->link=q;rn q->link=p;rn pre->link=q;rn rn elsern rn q->link=p;rn A.first=q;rn rn rn rn return A;rn rnrntemplaternvoid divide(Chain &A,Chain &B,Chain &C)rnrn if(C.first==0)rn rn A.first=0;rn B.first=0;rn rn elsern rn ChainNode *p,*pa;rn ChainNode *pb=0;rn p=C.first;rn if((p->data)%2)rn rn A.first=p;rn pa=A.first;rnrn rn elsern rn B.first=p;rn pb=B.first;rn rn p=p->link;rn while(p)rn rn if((p->data)%2)rn rn if (pa==0)rn rn A.first=pa;rn pa=A.first;rn rn elsern rn pa->link=p;rn rn pa=pa->link;rnrn rn elsern rn if(pb==0)rn rn B.first=p;rn pb=B.first;rn rn elsern rn pb->link=p;rn rn pb=pb->link;rnrn rn p=p->link;rn rn /*if((p->data)%2)rn rn A.first=p;rnrn rn elsern rn B.first=p;rn */rn rnrntemplaternclass ChainIterator rnrnpublic: rn T* Initialize(const Chain& c)rn rn location = c.first; rn if(location) rn return &location->data; rn return 0;rn rn T* Next( )rn rn if (!location) return 0; rn location = location->link; rn if (location) rn return &location->data; rn return 0;rn rnprivate:rn ChainNode *location;rn;int _tmain(int argc, _TCHAR* argv[])rnrn Chain L1,L2,L3,L4,L5; rn L1.Insert(0,1).Insert(1,3).Insert(2,5);rn L2.Insert(0,2).Insert(1,4).Insert(2,8).Insert(3,10); rn L3=merge(L1,L2);rn ChainIterator C;rn int *x1,*x2,*x3;rn x1=C.Initialize(L1);rn x2=C.Initialize(L2);rn x3=C.Initialize(L3);rn while(x3)rn rn cout<<*x3<<" ";rn x3=C.Next();rn cout< 论坛

没有更多推荐了,返回首页