输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
(hint: 请务必使用链表。)
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为两个整数n和m(0<=n<=1000, 0<=m<=1000):n代表将要输入的第一个链表的元素的个数,m代表将要输入的第二个链表的元素的个数。
下面一行包括n个数t(1<=t<=1000000):代表链表一中的元素。接下来一行包含m个元素,s(1<=t<=1000000)。
对应每个测试案例,
若有结果,输出相应的链表。否则,输出NULL。
5 2 1 3 5 7 9 2 4 0 0
1 2 3 4 5 7 9 NULL
import java.io.IOException; import java.io.StreamTokenizer; public class Main { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub StreamTokenizer stin = new StreamTokenizer(System.in); int m, n,value; while (stin.nextToken() != StreamTokenizer.TT_EOF) { m = (int) stin.nval; stin.nextToken(); n = (int) stin.nval; if (m == 0 && n==0) System.out.println("NULL"); else { ListNode head1 = null; ListNode head2 = null; ListNode p = null; for (int i = 0; i < m; i++) {// 初始化链表 stin.nextToken(); value = (int) stin.nval; if (i == 0) { head1 = new ListNode(); head1.value = value; head1.next = null; p = head1; } else { ListNode pNode = new ListNode(); pNode.value = value; pNode.next = null; p.next = pNode; p = pNode; } } for (int i = 0; i < n; i++) {// 初始化链表 stin.nextToken(); value = (int) stin.nval; if (i == 0) { head2 = new ListNode(); head2.value = value; head2.next = null; p = head2; } else { ListNode pNode = new ListNode(); pNode.value = value; pNode.next = null; p.next = pNode; p = pNode; } } ListNode head=merge(head1, head2); p = head; while (p!= null && p.next != null) { System.out.print(p.value + " "); p = p.next; } System.out.print(p.value); System.out.println(); } } } public static ListNode merge(ListNode head1,ListNode head2){ if (head1 == null) return head2; else if (head2 == null) return head1; ListNode head = null; if (head1.value < head2.value) { head = head1; head.next=merge(head1.next, head2); } else { head = head2; head.next=merge(head1, head2.next); } return head; } } class ListNode { int value; ListNode next; }C++代码
#include <stdio.h> using namespace std; class LinkNode{ public: int val; LinkNode *next; LinkNode(): val(0), next(NULL) {}; }; LinkNode* RetrieveList(int n){ int val; if(n <= 0) return NULL; LinkNode *head = NULL; LinkNode *cur = NULL; for(int i = 0; i < n; ++i){ scanf("%d", &val); if(head == NULL){ head = new LinkNode(); cur = head; cur->val = val; } else{ cur->next = new LinkNode(); cur = cur->next; cur->val = val; } } return head; } void PrintAndDestroyList(LinkNode* head){ if(head == NULL){ printf("NULL\n"); return; } LinkNode *cur = head; while(cur){ printf("%d", cur->val); cur = cur->next; delete head; head = cur; if(cur) printf(" "); } printf("\n"); } LinkNode* MergeList(LinkNode* head1, LinkNode* head2){ LinkNode* head = NULL; LinkNode** cur = &head; while(head1 && head2){ if(head1->val <= head2->val){ *cur = head1; head1 = head1->next; } else{ *cur = head2; head2 = head2->next; } cur = &((*cur)->next); } if(head1 || head2){ *cur = head1 ? head1 : head2; } return head; } int main(void){ int n, m; while(scanf("%d", &n) != EOF){ scanf("%d", &m); LinkNode *head1 = RetrieveList(n); LinkNode *head2 = RetrieveList(m); LinkNode* head = MergeList(head1, head2); PrintAndDestroyList(head); } return 0; }C代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node { int data; struct node *next; }linkNode; linkNode *initLinkList(linkNode *L) { L = (linkNode *)malloc(sizeof(linkNode)); if (NULL == L) { return NULL; } L->next = NULL; return L; } linkNode *createLinkList(linkNode *L, int n) { linkNode *tail = L; linkNode *p = NULL; while (n--) { p = (linkNode *)malloc(sizeof(linkNode)); scanf("%d", &p->data); tail->next = p; tail = p; } tail->next = NULL; return L; } linkNode *mergeLinkList(linkNode *LA, linkNode *LB) { linkNode *tmp = LA; linkNode *pa = LA->next; linkNode *pb = LB->next; while (pa!=NULL && pb!=NULL) { if(pa->data < pb->data) { tmp->next = pa; tmp = pa; pa = pa->next; } else { tmp->next = pb; tmp = pb; pb = pb->next; } } tmp->next = pa ? pa : pb; /*tmp直接指向剩余部分*/ return LA; } void printLinkList(linkNode *L) { linkNode *p = L; while (p->next != NULL) { if (p != L) { printf(" "); } p = p->next; printf("%d", p->data); } printf("\n"); } void destroyLinkList(linkNode *L) { linkNode *p = L->next; while ( p != NULL ) { L->next = p->next; free(p); p = L->next; } free(L); L = NULL; } int main(void) { int n, m; while(scanf("%d%d", &n, &m) != EOF) { if (m+n == 0) { printf("NULL\n"); } else { linkNode *LA = NULL; linkNode *LB = NULL; LA = initLinkList(LA); LB = initLinkList(LB); LA = createLinkList(LA, n); LB = createLinkList(LB, m); LA = mergeLinkList(LA, LB); printLinkList(LA); destroyLinkList(LA); /*因为在链接的过程中A指向了B,顺带着把B释放了*/ } } return 0; }
原文地址:http://blog.csdn.net/lvsaixia/article/details/40897549