首页 > 试题广场 >

给定两个升序排列的单向链表 a、 b ,请设计一个方法合并

[问答题]

给定两个升序排列的单向链表 a、 b ,请设计一个方法合并 a和 b 且合并后保持降序有序。

例如: a 5->10->15->20; b: 6->7->25

合并后为: 25->20->15->10->7->6->5

每次比较这两个升序链表的当前第一个结点,谁小,谁就先被摘下,实施头插入法插入到新链表的表头?
发表于 2017-08-21 10:47:17 回复(1)
struct ListNode {
    int val;
    ListNode* next;
};

ListNode* mergeList(ListNode* la, ListNode* lb) {
    if (la == NULL && lb == NULL) return NULL;
    if (la == NULL)
        return lb;
    else if (lb == NULL)
        return la;
    ListNode* lc = NULL;
    
    if (la->val > lb->val) {
        lc = la;
        lc->next = mergeList(la->next, lb);
    }
    else {
        lc = lb;
        lc->next = mergeList(la, lb->next);
    }
    return lc;
}
发表于 2021-10-09 11:07:06 回复(0)
代码运行正确
#include<stdio.h>  
#include<stdlib.h>  
  
typedef struct node{  
    char data;  
    struct node *next;  
}LNode;  
  
LNode *creat_linklist()  
{  
    LNode *p,*q,*head;  
    int i,n;  
    head=(LNode*)malloc(sizeof(LNode));  
    head->next=NULL;  
    p=head;  
    q=p;  
    //printf("请输入单链表长度:\n");  
    scanf("%d",&n);  
    //printf("请输入值:\n");  
    for(i=1;i<=n;i++)  
    {  
        p=(LNode*)malloc(sizeof(LNode));  
        scanf("%d",&p->data);  
        p->next=NULL;  
        q->next=p;  
        q=p;  
    }  
    return head;  
}  
  
void merge(LNode *A,LNode *B,LNode **C)  
{  
    LNode *p,*q,*s;  
    p=A->next;  
    q=B->next;  
    *C=A;  
    (*C)->next=NULL;  
    free(B);  
    while(p!=NULL&&q!=NULL)  
    {  
        if(p->data<q->data)  
        {  
            s=p;  
            p=p->next;  
        }  
        else  
        {  
            s=q;  
            q=q->next;  
        }  
        s->next=(*C)->next;  
        (*C)->next=s;  
    }  
    if(p==NULL)  
        p=q;  
    while(p!=NULL)  
    {  
        s=p;  
        p=p->next;  
        s->next=(*C)->next;  
        (*C)->next=s;  
    }  
}  
  
void print(LNode *p)  
{  
    p=p->next;  
    while(p!=NULL)  
    {  
        printf("%5d",p->data);  
        p=p->next;  
    }  
    printf("\n");  
}  
  
int main()  
{  
    LNode *A,*B,*C;  
    //printf("*** 创建单链表A ***\n");  
    A=creat_linklist();  
    //printf("您创建的单链表A为:\n");  
    //print(A);  
    //printf("\n");  
    //printf("*** 创建单链表B ***\n");  
    B=creat_linklist();  
   // printf("您创建的单链表B为:\n");  
    //print(B);  
    //printf("\n合并之后的单链表C为:\n");  
    merge(A,B,&C);  
    print(C);  
    return 0; 
}  
编辑于 2017-08-22 14:36:15 回复(1)
public static void mergeList(Node a,Node b){
		Stack<Integer> merged=new Stack<>();
		Node p=a,q=b;
		while(p!=null&&q!=null){
			if(p.value<q.value){
				merged.push(p.value);
				p=p.next;
			}
			if(q.value<p.value){
				merged.push(q.value);
				q=q.next;
			}
		}
		while(p != null){
			merged.push(p.value);
			p=p.next;
		}
		while(q!=null){
			merged.push(q.value);
			q=q.next;
		}
		Node head=new Node();
		head.setValue(merged.pop());
		head.setNext(null);
		Node temp=head;
		while(!merged.isEmpty()){
			Node t=new Node();
			t.setNext(null);
			t.setValue(merged.pop());
			temp.setNext(t);
			temp=t;
		}
		
	}

发表于 2017-08-22 00:32:35 回复(0)
package algorithm;  public class ConvertLink { private static class Node { int value;  Node next;  } private static void print(Node node) { while (node != null) {
            System.out.println(node.value);  node = node.next;  }
    } /**  * 链表反转  * @param node  * @return  */  private static Node convertLink(Node node) { if (node == null || node.next == null) { return node;  }
        Node preNode = node;  Node editNode = node.next;  Node nextNode = editNode.next;  //循环开始之前必须要先把原来链表的头的next置为空,因为这个节点将是新链表的尾节点  preNode.next = null;  while (editNode != null) {
            editNode.next = preNode;  preNode = editNode;  editNode = nextNode;  if (nextNode != null) {
                nextNode = nextNode.next;  }
        } return preNode;  } /**  * 传入两个链表都是降序排列  *  * @param node1  * @param node2  * @return  */  private static Node mergeTwoLink(Node node1, Node node2) {
        Node nextNode;//先将下一个节点保存起来  Node insertNode;  Node editNode = node1;  Node headNode = node1;  //先判断头节点那个大  if (node1.value > node2.value) {
            insertNode = node2;  }else{
            insertNode = node2.next;  node2.next = node1;  headNode = node2;  editNode = node2;  } while (insertNode != null) {
             nextNode = insertNode.next;  while (editNode != null && editNode.next != null && editNode.next.value > insertNode.value)
                 editNode = editNode.next;  Node editNextNode = editNode.next;  editNode.next = insertNode;  insertNode.next = editNextNode;  insertNode = nextNode;  } return headNode;  } public static void main(String[] args) {
        Node head = new Node();  Node next1 = new Node();  Node next2 = new Node();  Node next3 = new Node();  Node next4 = new Node();   head.value = 5;  head.next = next1;   next1.value = 10;  next1.next = next2;   next2.value = 15;  next2.next = next3;   next3.value = 20;  next3.next = next4;   next4.value = 25;  next4.next = null;    Node head1 = new Node();  Node next21 = new Node();  Node next22 = new Node();   head1.value = 2;  head1.next = next21;   next21.value = 4;  next21.next = next22;   next22.value = 8;  next22.next = null;     System.out.println("convert before ");  System.out.println("node 1");  print(head);  System.out.println("node 2");  print(head1);  head = convertLink(head);  head1 = convertLink(head1);  head = mergeTwoLink(head1,head);  System.out.println("convert after");  print(head);    }

}

发表于 2017-08-19 19:07:40 回复(0)