leetcode 21. Merge Two Sorted Lists
来源:互联网 发布:盈建科软件最新版下载 编辑:程序博客网 时间:2024/05/17 01:49
每个链表一个游标,把小的数插入结果就行了。
#include <stdio.h>#include <stdlib.h>#include <stddef.h>struct ListNode* add(struct ListNode* l1,int num);struct ListNode* removeNthFromEnd(struct ListNode* head, int n);struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2);int print(struct ListNode* l1); struct ListNode { int val; struct ListNode *next; };int main(){ struct ListNode *l1=NULL; struct ListNode *l2=NULL; struct ListNode *merged=NULL; l1=add(l1,1); add(l1,4); add(l1,6); l2=add(l2,2); add(l2,4); add(l2,9); add(l2,10); print(l1); print(l2); //result=addTwoNumbers() printf("-------------------\n"); //l1=removeNthFromEnd(l1,2); merged=mergeTwoLists(l1,l2); print(merged); return 0;}struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode* pointer1=l1; struct ListNode* pointer2=l2; struct ListNode* merged=NULL; struct ListNode* temp=NULL; struct ListNode* mergedPointer=NULL; while(pointer1!=NULL||pointer2!=NULL) { if(pointer1!=NULL&&pointer2!=NULL) { if(pointer1->val<=pointer2->val) { temp=(struct ListNode*)malloc(sizeof(struct ListNode)); temp->next=NULL; temp->val=pointer1->val; pointer1=pointer1->next; } else { temp=(struct ListNode*)malloc(sizeof(struct ListNode)); temp->next=NULL; temp->val=pointer2->val; pointer2=pointer2->next; } } else if(pointer1!=NULL) { temp=(struct ListNode*)malloc(sizeof(struct ListNode)); temp->next=NULL; temp->val=pointer1->val; pointer1=pointer1->next; } else { temp=(struct ListNode*)malloc(sizeof(struct ListNode)); temp->next=NULL; temp->val=pointer2->val; pointer2=pointer2->next; } if(merged==NULL) { merged=temp; mergedPointer=temp; } else { mergedPointer->next=temp; mergedPointer=temp; } } return merged;}struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { struct ListNode* pointer=head; int i,count; i=0; count=0; if(pointer->next==NULL&&n==1) return NULL; while(pointer!=NULL) { pointer=pointer->next; count++; } pointer=head; if(n==count) { head=head->next; return head; } while(pointer->next!=NULL) { if(i==count-n-1) break; pointer=pointer->next; i++; } pointer->next=pointer->next->next; return head;}int print(struct ListNode* l1){ struct ListNode* pointer; pointer=l1; while(pointer!=NULL) { printf("%d ",pointer->val); pointer=pointer->next; } return 0;}struct ListNode* add(struct ListNode* l1,int num){ struct ListNode* pointer; struct ListNode* newNode; pointer=l1; if(l1==NULL) { newNode=(struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val=num; newNode->next=NULL; l1=newNode; } else { while(pointer->next!=NULL) { pointer=pointer->next; } newNode=(struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val=num; newNode->next=NULL; pointer->next=newNode; } return l1;}/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; *//*struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {}*/
0 0
- 21. Merge Two Sorted Lists leetcode(lists)
- [LeetCode]21.Merge Two Sorted Lists
- LeetCode --- 21. Merge Two Sorted Lists
- LeetCode 21.Merge Two Sorted Lists
- [Leetcode] 21. Merge Two Sorted Lists
- 【LeetCode】21.Merge Two Sorted Lists
- 【leetcode】21. Merge Two Sorted Lists
- [leetcode] 21.Merge Two Sorted Lists
- [leetcode] 21.Merge Two Sorted Lists
- [leetcode] 21. Merge Two Sorted Lists
- <LeetCode OJ> 21. Merge Two Sorted Lists
- 21. Merge Two Sorted Lists LeetCode
- leetcode 21. Merge Two Sorted Lists
- Leetcode:21. Merge Two Sorted Lists(JAVA)
- Leetcode ☞ 21. Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists
- [LeetCode]21. Merge Two Sorted Lists
- 【LeetCode】21. Merge Two Sorted Lists
- next_permutation
- android应用中去掉标题栏的三种方法
- 使用VNC进行远程控制
- 剑指offer——面试题1:赋值运算符函数
- java 8 之Streams 解说
- leetcode 21. Merge Two Sorted Lists
- 《CSS权威指南》——笔记1(概述)
- Android SQLiteDatabase使用详细教程
- LightOJ 1405 The Great Escape 最大流
- 【C】少年时代的经典游戏:三子棋
- 闭包的一个注意事项
- [Alfred]替代Spotlight
- Activity的生命周期
- VC中ADO Data控件的使用