LeetCode(Linked List)21. Merge Two Sorted Lists

迈不过友情╰ 2023-09-24 19:27 169阅读 0赞

LeetCode(Linked List)

1.问题

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

Example 1:

b3bed9f14b3c40828c590519970d959e.png

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:

Input: list1 = [], list2 = []
Output: []

Example 3:

Input: list1 = [], list2 = [0]
Output: [0]

Constraints:

  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.

2. 解题思路

方法1:

取出list1和list2的值,进行倒序排序,之后新建结点,从结点前面添加数据,之后返回新的结点

方法2:

1.创建新结点,最后的head.next删除创建的结点
2.定义cur做为指针辅助
3.如果list1和list2不为null,list1和list2的值进行比较,cur.next就是最小的值,并移动list1或list2的下一个值
4.如果list1不为null,curr.next=list1
5.如果list2不为null,curr.next=list2
6.返回值为去掉结点head的头节点(0),如果head的头节点为0,无法分配下一个字段next

方法3:

利用递归

3. 代码

代码1:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
  13. ListNode head = null;
  14. List<Integer> list = new ArrayList<>();//取出list1和list2的值存入list
  15. while (list1 != null) {
  16. list.add(list1.val);
  17. list1= list1.next;
  18. }
  19. while (list2 != null) {
  20. list.add(list2.val);
  21. list2= list2.next;
  22. }
  23. Collections.sort(list);//进行升序排序
  24. Collections.reverse(list);//倒序是为了从后向前添加到ListNode中
  25. ListNode n = null;
  26. for(int new_data:list){
  27. n = new ListNode(new_data);
  28. n.next=head;
  29. head = n;
  30. }
  31. return n;
  32. }
  33. }

本地代码运行

  1. class LinkedList {
  2. static ListNode head;
  3. static class ListNode {
  4. int val;
  5. ListNode next;
  6. ListNode(int d) {
  7. val = d;
  8. next = null;
  9. }
  10. }
  11. public static ListNode mergeTwoLists(ListNode list1, ListNode list2){
  12. ListNode head = null;
  13. List<Integer> list = new ArrayList<>();
  14. while (list1 != null) {
  15. list.add(list1.val);
  16. list1= list1.next;
  17. }
  18. while (list2 != null) {
  19. list.add(list2.val);
  20. list2= list2.next;
  21. }
  22. Collections.sort(list);
  23. Collections.reverse(list);
  24. ListNode n = null;
  25. for(int new_data:list){
  26. n = new ListNode(new_data);
  27. n.next=head;
  28. head = n;
  29. }
  30. return n;
  31. }
  32. static void printList(ListNode node) {
  33. while (node != null) {
  34. System.out.print(node.val + " ");
  35. node = node.next;
  36. }
  37. }
  38. public static void main(String[] args) {
  39. LinkedList list1 = new LinkedList();
  40. list1.head = new ListNode(1);
  41. list1.head.next = new ListNode(3);
  42. list1.head.next.next = new ListNode(4);
  43. list1.printList(head);
  44. System.out.println("\n");
  45. LinkedList list2 = new LinkedList();
  46. list2.head = new ListNode(1);
  47. list2.head.next = new ListNode(3);
  48. list2.head.next.next = new ListNode(4);
  49. list2.printList(head);
  50. System.out.println("\n");
  51. list2.printList( mergeTwoLists(list1.head,list2.head));
  52. }

运行结果

  1. 1 3 4
  2. 1 3 4
  3. 1 1 3 3 4 4

代码2:

1.创建新结点,最后的head.next删除创捷的结点
2.定义cur做为指针辅助
3.如果list1和list2不为null,list1和list2的值进行比较,cur.next就是最小的值,并移动list1或list2的下一个值
4.如果list1不为null,curr.next=list1
5.如果list2不为null,curr.next=list2
6.返回值为去掉结点head的头节点(0),如果head的头节点为0,无法分配下一个字段next

  1. public class Solution {
  2. public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
  3. ListNode head = new ListNode(0);//1.创建新结点,最后的head.next删除创建的结点
  4. ListNode cur=head;//2.定义cur做为指针辅助
  5. while (list1 != null && list2 != null) {
  6. //3.如果list1和list2不为null,list1和list2的值进行比较,cur.next就是最小的值,并移动list1或list2的下一个值
  7. if(list1.val<=list2.val){
  8. cur.next=list1;
  9. list1=list1.next;
  10. }else {
  11. cur.next=list2;
  12. list2 = list2.next;
  13. }
  14. cur=cur.next;
  15. }
  16. if(list1!=null){
  17. //4.如果list1不为null,curr.next=list1
  18. cur.next=list1;
  19. }
  20. if(list2!=null){
  21. //5.如果list2不为null,curr.next=list2
  22. cur.next=list2;
  23. }
  24. return head.next;//6.返回值为去掉结点head的头节点(0),如果head的头节点为0,无法分配下一个字段next
  25. }
  26. }

解题思路基本相同

  1. public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
  2. ListNode head = new ListNode(0);
  3. ListNode cur = head;
  4. while(list1 != null && list2 != null) {
  5. if(list1.val <= list2.val) {
  6. cur.next = list1;
  7. list1 = list1.next;
  8. } else {
  9. cur.next = l2;
  10. list2 = list2.next;
  11. }
  12. cur = cur.next;
  13. }
  14. while (list1 != null) {
  15. cur.next = l1;
  16. list1 = l1.next;
  17. cur = cur.next;
  18. }
  19. while(list2 != null) {
  20. cur.next = list2;
  21. list2 = list2.next;
  22. cur = cur.next;
  23. }
  24. return head.next;
  25. }

代码3:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
  13. ListNode res=null ;
  14. if(list1==null){
  15. return list2;
  16. }
  17. if(list2==null){
  18. return list1;
  19. }
  20. if(list1.val < list2.val){
  21. res=list1;
  22. res.next = mergeTwoLists(list1.next,list2) ;
  23. }
  24. else{
  25. res=list2;
  26. res.next = mergeTwoLists(list1,list2.next) ;
  27. }
  28. return res ;
  29. }
  30. }

发表评论

表情:
评论列表 (有 0 条评论,169人围观)

还没有评论,来说两句吧...

相关阅读