1. Single-chain table of the lead Node,
// Define the class testnode {public int data; Public testnode next = NULL; Public testnode (INT data) {This. data = data ;}// public class merge {public static void main (string [] ARGs) {testnode n00 = new testnode (0 ); testnode n1 = new testnode (1); n00.next = N1; testnode n12 = new testnode (2); n1.next = n12; // system. out. println (n1.next. data); testnode N13 = new testnode (5); n12.next = N13; testnode N14 = new testn Ode (6); n13.next = N14; testnode N20 = new testnode (0); testnode n2 = new testnode (3); n1_next = n2; testnode n22 = new testnode (7 ); n2.next = n22; testnode n23 = new testnode (8); n22.next = n23; testnode n24 = new testnode (9); n23.next = n24; testnode n25 = new testnode (10 ); n24.next = n25; testnode H = Merge (n00, N20); testnode P = H. next; while (P! = NULL) {system. out. println (P. data); P = P. next ;}} public static testnode Merge (testnode N1, testnode N2) {testnode p1 = n1.next; testnode head = N1; testnode P2 = n2.next; testnode P3 = N1; while (P1! = NULL & p2! = NULL) {If (p1.data <= p2.data) {p3.next = p1; P3 = p1; P1 = p1.next;} else {p3.next = P2; P3 = P2; p2 = p2.next;} If (p1 = NULL & p2! = NULL) {p3.next = P2;} else {p3.next = p1;} return head ;}}
2. A single-chain table with no leading nodes
// The two ordered linked lists of nodes without headers are merged into an ordered linked list public class listmerge {public static void main (string [] ARGs) {node n1 = new node (1 ); node n2 = new node (2); n1.next = n2; node N3 = new node (5); n2.next = N3; node M1 = new node (4 ); node m2 = new node (6); m1.next = m2; node m3 = new node (9); m2.next = m3; node M4 = new node (10); m3.next = M4; node q = Merge (N1, M1); While (Q! = NULL) {system. out. println (Q. data); q = Q. next ;}} public static node Merge (node N1, node N2) {node p1 = N1; node P2 = n2; node P3 = NULL; node head = NULL; if (p1.data <p2.data) {head = N1; P1 = p1.next; // you forgot to write this sentence at the beginning} else {head = n2; P2 = p2.next; // Similarly, do not forget to write this sentence .} p3 = head; while (P1! = NULL & p2! = NULL) {If (p1.data <p2.data) {p3.next = p1; // the value in the first linked list linked by the P3 pointer, link the node in the second linked list to the sorted chain P3 = p1; // move P3 forward to a p1 = p1.next; // move P1 forward to a} else {p3.next = P2; // The P3 pointer connects the value in the second linked list and links the node in the second linked list to the sorted chain P3 = P2; // P3 moves forward a P2 = p2.next; // P2 move one forward} If (p1 = NULL) {// when P1 has no elements, that is, all elements in P1 have been linked to p3.next = P2 ;} else {// p3.next = p1;} return head when P2 has no elements ;}}
// Node class node {int data; node next = NULL; Public node (INT data) {This. Data = data ;}
Where, there is a problem with the running of nodes that do not take the lead. If you do not know where the node is, you need to debug the node again... This is a very strange mistake. I don't know where it is wrong. I hope I have a high finger.