aboutsummaryrefslogtreecommitdiff
path: root/merge_between_linked_list.c
blob: 4115b208f3f305c2a76a0060598d7b2758f5b550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 1669. Merge In Between Linked Lists
#include "leetcode.h"

/*
 * given two linked lists 'l1' and 'l2' of sizes 'n' and 'm' respectively.
 * remove 'l1' nodes from the a'th node to the b'th node, and put 'l2' in their
 * place. the blue edges and nodes in the following figure indicate the result.
 */

struct ListNode {
  int val;
  struct ListNode *next;
};

struct ListNode *mergeInBetween(struct ListNode *list1, int a, int b,
                                struct ListNode *list2) {
  struct ListNode *nodea = list1, *nodeb = list1;
  int cnt = 0;
  for (struct ListNode *node = list1; node; node = node->next) {
    if (cnt == a - 1)
      nodea = node;
    if (cnt == b) {
      nodeb = node->next;
      break;
    }
    cnt++;
  }
  struct ListNode *tmp = list1;
  bool is_change = false;
  while (tmp) {
    if (tmp == nodea)
      tmp->next = list2;
    if (!tmp->next && !is_change) {
      tmp->next = nodeb;
      is_change = true;
    }
    tmp = tmp->next;
  }
  return list1;
}