How to concatenate two linked lists?

Can anyone help me with this? I'm stuck. I'm trying to concatenate two linked lists. I've created a method that adds a node to the beginning of a list and a second method to concatenate. My first method works, but not my second...
First method:
public static Node insertFirst(Node head, Node node){
          Node newNode = new Node();          // declare and initialize newNode.
          newNode = node;                    // store the new item in the new node.
          newNode.link = head;               // newNode.link points to the head.
          head = newNode;                    // update head to be newNode.
          return head;
     }Here is where I have my problem:
public static Node mergeTwoLists(Node a, Node b){
          Node heada = a;
          Node headb = b;
          if(heada == null)
               return headb;                    //if heada is null then returns list headb.
          else if(headb == null)
               return heada;                    //if headb is null then returns list heada.
          else if(heada == null && headb == null)     //if both lists are null, returns null.
               return null;
          else if(heada.link == null){     //only one element in heada.
               headb = insertFirst(b, a);               // inserts the element in heada into headb.
               return headb;
          }//end else if(heada.link == null).
          else if(headb.link == null){     //only one element in headb.
               heada = insertFirst(a,b);               // inserts the element in headb into heada.
               else {
                    mergeTwoLists(heada.link, headb);
                    return heada;
               }//end else
          return heada;
          }//end mergeTwoLists method.I think in this method I'm only adding the nodes if there is only one node in one of the lists. But if there are more than one element in a list...I'm confused as to how to code the last else statement that contains the recursive call.
Can someone help?
Thanks
aiki985

Can't you just point the tail of A to the head of B?
I don't think I'd do this recursivly at all. Use a
while look to find the last node in A and then link
it to whatever the first element in B is.
while(A.next != null){
A = A.next;
A.next = B;
Java that up a bit and check for nulls and it should
work.o.k. thanks...let me try that. It does seem awkward recursively...
aiki985

Similar Messages

  • How to create two linked lists (Master/Detail or Parent/Child)

    I need to create a couple of related lists in a Master/Detail approach. Users are supposed to choose one "Master" Option, and "Detail" column should be refreshed to display only the items that are strictly related to the chosen option
    in "Master". Any posible solution?
    Thanks in advance.

    One option is to connect two web parts. This will give you option button for your master list web part and based on the value selected your results will be filtered in the detailed list web part. Here is the example. Though it is for SP 2010 it should work
    the same in SP 2013.
    http://sarahlhaase.wordpress.com/2012/05/21/connecting-web-parts-with-a-selector-and-a-detail-pane-sharepoint-2010-version/
    Second option is with some customization using Jquery.
    http://summit7systems.com/creating-a-parentchild-list-relationship-in-sharepoint-2013/
    Amit

  • Merging two linked lists of integers that are sorted

    hey guys,
    i need to write a method to merge two linked lists of integers that are sorted into ascending order. the results should be a third linked list that is the sorted combination of the original lists.
    can someone help me start this? Basically, the method will be taking in two sorted linked lists of integers and merge them into one big sorted linked list.
    Im just looking to get some algorithm ideas and maybe one or two lines of pseudocode to get me started.
    thanks

    i can't destroy the original lists so im gonna need
    to create a new sorted list. then since both of the
    lists im using are sorted, i'll just have to copy one
    of them into the new list i created. Then i will get
    an item from the unused list and search that node in
    the new copied list and insert in appropriate
    position using the two Nodes...example(prev and
    curr).That can work. I'd probably do it by initializing a new list and compare the first items in each of the original lists, inserting the smaller in my new list and advancing to the next element in the original list from which I took the item, repeating the process until I copied each item from both original lists. Don't forget to take into account that you will reach the end of one list before the other.

  • How to make a linked list?

    I've seen how to implement a linked list in oracle ? but it's not what I'm interested in.
    First of all, it is just a curiosity, no serious task behind it. I think I've almost made a linked list (of chars) type, however it misses the notion of empty list and thus I virtually cannot construct it.
    create or replace type LString as object
      head Char,
      tail ref LString,
      member function cons(
        p_x Char)
        return LString
    create or replace type body LString is
      member function cons(
        p_x Char)
        return LString
      is
        xs LString;
      begin
        xs := LString(head => p_x, tail => self.tail);
        return xs;
      end;
    end;I need to be able to make empty list to construct other lists, for example:
    empty_lstring.cons('H').cons('i').cons('!')
    How can I do it?
    Edited by: 922141 on 12.07.2012 7:02

    I need to be able to make empty list to construct other lists, for example:
    empty_lstring.cons('H').cons('i').cons('!')
    Why ? Can you mention one procedural language who can do this? I can't! Can you?
    Sybrand Bakker
    Senior Oracle DBA

  • How to concatenate two column in ALV

    dear,
    How to concatenate two ALV columns
    yatendra sharma

    dear
    I have to concatenate 3 fields
    PERFORM fill_fields_of_fieldcatalog
        USING 'IT_FINAL' 'NAME2' ' ' ' ' 'Customer Address' .
      PERFORM fill_fields_of_fieldcatalog
        USING 'IT_FINAL' 'STRAS' ' ' ' ' 'Street' .
      PERFORM fill_fields_of_fieldcatalog
        USING 'IT_FINAL' 'ORT01' ' ' ' ' 'City'.
    how can we join them
    Yatendra

  • How to concatenate two colums into one single column

    I need some ideas to concatenate two different columns into one single column using a set of distinct values.
    For Example,
    Customer Product Number
    xyz A 1
    xyz B 2
    xyz B 1
    AAA C 7
    AAA A 1
    The result should look like this,
    Customer Value
    xyz A1 B2 B1
    AAA C7 A1
    How would I group this into once value ?
    Thanks in advance ...

    Tom's discussion of writing your own aggregate routines
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:2196162600402
    starts off with a link to the 8i alternatives
    "see
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:229614022562
    for 8i methods (not aggregates)"
    Unforutnately, it's a lot more work in 8i.
    Justin

  • How to use circular linked list in pl/sql?

    Hi all,
    how to use the circular linked list on pl/sql programming.
    thanks in advance
    Rgds,
    B@L@

    d balamurugan wrote:
    Hi,
    I needed this concept for the below example
    TABLE_A have the columns of
    ID COL_1 COL_2 COL_3 COL_4 COL_5 COL_6 COL_7
    1....Y.........N........N.........Y........ N........N........ N
    2....N.........N....... N.........Y.........N........N.........Y
    in the above data
    for id 1 i will need to take the value for COL_4, then i will check the next availability of Y through out through out the remaining columns, so next availability is on COL_1 so, i need to consider COL_4 which already Y and also i need to consider COL_5, COL_6, COL_7 as Y.
    for id 2 if i need COL_7 then i need to come back on circular way and need to check the next availability of Y and need to take the columns having N before the next availability of YAnd... even after all that description... you haven't given any indication of what the output should look like.
    Taking a wild guess on my part... something like this would do what you appear to be asking...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 1 as id, 'Y' as col1, 'N' as col2, 'N' as col3, 'Y' as col4, 'N' as col5, 'N' as col6,'N' as col7 from dual union all
      2             select 2, 'N', 'N', 'N', 'Y', 'N', 'N', 'Y' from dual)
      3  --
      4  -- END OF TEST DATA
      5  --
      6  select id
      7        ,rcol
      8        ,case when instr(cols,'Y',rcol+1) = 0 then instr(cols,'Y')
      9              else instr(cols,'Y',rcol+1)
    10         end as next_Y_col
    11  from   (select id, rcol, col1||col2||col3||col4||col5||col6||col7 as cols
    12          from t, (select &required_col as rcol from dual)
    13          where id = &required_id
    14*        )
    SQL> /
    Enter value for required_col: 4
    old  12:         from t, (select &required_col as rcol from dual)
    new  12:         from t, (select 4 as rcol from dual)
    Enter value for required_id: 1
    old  13:         where id = &required_id
    new  13:         where id = 1
            ID       RCOL NEXT_Y_COL
             1          4          1
    SQL> /
    Enter value for required_col: 7
    old  12:         from t, (select &required_col as rcol from dual)
    new  12:         from t, (select 7 as rcol from dual)
    Enter value for required_id: 2
    old  13:         where id = &required_id
    new  13:         where id = 2
            ID       RCOL NEXT_Y_COL
             2          7          4
    SQL>If that's not what you want... then it's time you started learning how to ask questions properly.

  • How to concatenate two AttributedString

    Hi Everyone
    The class java.text.AttributedString looks interesting, but I would like to create one not all at once.
    I would like for example to create the AttributedString and addAttributes to it, then append some text at the end and add some attributes to this new text and so on, as I'm reading through a file for example...
    I searched a lot and could not find a way to append a string to an already existing AttributedString or to concatenate two AttributedString.
    I looked at the source and found that there is a constructor AttributedString(AttributedCharacterIterator[] iterators), that would be perfect but the problem is that this constructor is not public (it's "package"). Anyone knows why this constructor is not public or how to do it another way.
    thanks
    Adam

    If I do this, I need to store all the attributes of my text when I read it and then apply them to the text. That's my plan B actually, because I don't want to store the attributes myself somewhere and then transfer them in the AttributedString. I would prefer to store them directly in the AttributedString as I read them.
    The other way would be to read the text twice, first to get the text, then to add the attributes, but then I would have to shift the indexes if I removed some tags in the original text.
    Anyway, I appreciate your remarks, it makes me think differently on the problem and see new alternatives...

  • How to configure two links between switches SG300

    Hello!
    I have two SG300 serie switches and two Gigabit connection between them. How do I configured these two links to work toghether like a one 2 Gigabit channel?
    thanks a lot.

    Hi Angel,
         I think what you are trying to do is called a LAG on the SG300. This option is located under the "Port Management" tab and you can manage which ports you want to be in the LAG under "Link Aggregation" and there you will see "LAG Management" where you can edit your LAG 1 with the 2 ports that needs to act as one 2 Gigabit port.
    I hope that was able to help you as that is what I have gathered from your question. Please reply back if you still need more assistance.
    Thanks,
    Brian Ng

  • How to implement a linked list in oracle ?

    HI All
    I want to know if there a way to implement a linked list in oracle ?
    Thanks in Advanced
    Naama

    A linked list is an array of the relevant data plus an indicator of the next and previous entries, right?
    Sounds easily achievable with any of the plsql collection types.
    The simplest would be a plsql associative array of a record
    Assignments of records in collections is always a bit hamfisted compared to sql objects types though (IMO).
    Something like ?
    DECLARE
    TYPE r_payload IS RECORD
    (col1 number,
      col2 number);
    TYPE r_array_entry is RECORD
    (prev_entry PLS_INTEGER,
      next_entry PLS_INTEGER,
      payload    r_payload);
    TYPE t_array IS TABLE OF r_array_entry INDEX BY PLS_INTEGER;
    v_array t_array;
    BEGIN
    NULL;
    END; 
      The use case for such a structure in properly coded SQL & PL/SQL is possibly the harder question.

  • How to concatenate two fields CSKS-KOSTL & CSKT-LTXT in BEx Query?

    Hi,
    I have a requirement to concatenate two fields CSKS-KOSTL (Cost Center) and CSKT-LTXT (Cost Center Text) and show them as one field in the BEx query.
    I do not know what to do, so please help me out.
    Thanks in advance.
    Regards
    Harman

    Hi Harman,
    If cost center is a masterdata, it can have its own text..i doubt why do u have 2 different fileds..
    Anyways if its a masterdata object maitaining its own text, then in the query properties under Display tab select Key&Text option this give you both key and text values in the output.
    If its two different fileds then its better to use concatenate in the transformation.
    Regards,
    Geetha

  • How to Concatenate two rows of same table

    Hi Friends
    I have table
    No Name Id
    1 Raju 6
    2 Dhanshree 7
    3 Shital 6
    4 Priya 7
    I want the query that should display
    Raju Dhanshree.
    Shital Priya
    That Means whenever 6,7 number come the query should Concatenate The name colume

    1 150 IEEE TRANSA CTIONS ON MICR O W A V E THEOR Y AND TECHNIQ UES, V OL. 50, NO. 1, J ANU AR Y 2002
    Times-Roman 4
    2 A Miniaturized MMIC Analog Phase Shifter Using
    Times-Roman 16
    3 T w o Quarter-W a v e-Length T ransmission Lines
    Times-Roman 16
    4 Hitoshi Hayashi
    Times-Roman 7
    5 , Member , IEEE
    Times-Italic 7
    6 , T adao Nakaga w a
    Times-Roman 7
    7 , Member , IEEE
    Times-Italic 7
    8 , and Katsuhik o Araki
    Times-Roman 7
    9 Abstract—
    Times-BoldItalic 6
    10 This paper describes a miniaturized monolithic-mi-
    Times-Bold 6
    11 cr o w a v e integrated-cir cuit (MMIC) analog phase shifter using tw o
    Times-Bold 6
    12 quarter-wa v e-length transmission lines. A con v entional analog
    Times-Bold 6
    13 phase shifter employs an analog phase-shifter topology using a
    Times-Bold 6
    14 3-dB 90
    Times-Bold 6
    15 branch-line h ybrid r equiring f our quarter-wa v e-length
    Times-Bold 6
    16 transmission lines. Thus, in the f irst stage of our study , w e
    Times-Bold 6
    17 pr esent a new analog phase-shifter topology using only tw o
    Times-Bold 6
    18 quarter-wa v e-length transmission lines. The phase shifter her e
    Times-Bold 6
    19 has only one-half as many transmission lines as a con v entional
    Times-Bold 6
    20 analog phase shifter using a 3-dB 90
    Times-Bold 6
    21 branch-line h ybrid, and the
    Times-Bold 6
    22 cir cuit can be miniaturized to less than one-f ourth as compar ed to
    Times-Bold 6
    23 the con v entional analog phase shifter . Furthermor e, we sho w that
    Times-Bold 6
    24 the operating fr equency range of the phase shifter is v ery wide and
    Times-Bold 6
    25 can obtain lar ge phase v ariation with small capacitance v ariation.
    Times-Bold 6
    26 Next, an experimental
    Times-Bold 6
    27 -band MMIC analog phase shifter is
    Times-Bold 6
    28 pr esented. A phase shift of mor e than 180
    Times-Bold 6
    29 and an insertion loss
    Times-Bold 6
    30 of 3.6
    Times-Bold 6
    31 1.1 dB ar e obtained at the fr equency range fr om 12 to
    Times-Bold 6
    32 14 GHz. The chip size of the experimental MMIC phase shifter is
    Times-Bold 6
    33 less than 3.0 mm
    Times-Bold 6
    34
    How will use on this data

  • Comparing two linked lists

    Hi all,
    I am nearing the completion of a program and have been snagged on a problem. The problem is have 2 lists, i want to see if list2 contains all the elements list1 conatins. If it does return true, if not return false. I have tried and tried but cant come up with a simple solution. Hopefully someone out there is able to help me.
    Cheers Dave.

    It either does or doesn't have elements, and if it
    does it contains more then 2, i have tested for
    this.
    I also put a System.out.println(list); before the if
    statement which returns the list [ ] being empty but
    then carries out the statement even though the list
    is == null.Nope, a list variable is either null, or not. When the reference is not
    null, the list can be empty, can contain one element or more elements.
    Being empty does not imply null. Try something like this:if (list != null && list.size() > 1) //remove two elementskind regards,
    Jos

  • How to print two alv lists in one spool request

    Hello,
    I have made a report which has one alv list based on CL_SALV_TABLE and a message box displayed in a splitter container based on SBAL. In dialog mode everything is fine.
    If I run the report in batch mode, I have put the alv table to
    list_display = abap_true.
    For printing the messages I am using FM BAL_DSP_LOG_PRINT. It has this nice parameter
    i_s_list_append = abap_true.
    Unfortunatly the messages are not appended to the alv list, nor are they in the same spool request.
    I think the system will put it only into the first spool request, when the name, the printer, the number of prints and the format are the same and of couse, if the spool request is still open for appenfing (which it is).
    The name and the format are not the same in my case. So I tried to set the name to TEST using the print options of the FM
    ls_print_options-print_ctrl-pri_params-plist = 'TEST'.
    But unfortunately it is not working. No changes to the name, when I run the report in batch mode.
    Who has experiance with that? Why are both lists not in the same spool request? Are my findings right? Why isn't the name changed?
    Thank you for your help,
    Peter

    do 2 times
    perform write_form.
    end do.
    for header u can give condtion in form.
    use if condition in the editor...
    this will help u...
    Reward IF....... <= IF what? Points removed... read [the rules|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/home/rulesofEngagement]!
    Regards
    Anbu
    Edited by: Julius Bussche on Jul 17, 2008 7:01 PM

  • How to concatenate two different strings?

    I am facing problem in concatenation of two different strings.Basic behind two strings is that 1st string consist today's date (ddmmyy) and second one is two digit number from a loop (01-99).So that I will come with 8 digit unique number(20100801) every time I hit it.
    Can u please give any reference for this?
    I will be very thankful !!
    With Regards,
    Aman Aggarwal

    ... or you could use StringBuilder...
    or you could use the formatting functionality of java.util.Formatter ...
    You need to be more specific about your problem. Concatenating string is the one of the simplest things one can do in Java. If you really are having a problem with it, you should be clearer about your problem. Post a SSCCE.

Maybe you are looking for