Skip to content

Commit a7776cf

Browse files
committed
Day-19 linkedlist Problems
1 parent f6f3dbf commit a7776cf

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* We have two numbers represented by a linked list,
3+
* where each node contains a single digit. The digits are stored in reverse order,
4+
* such that the 1's digit is at the head of the list.
5+
* Write a function that adds the two numbers and returns the sum as a linked list.
6+
*
7+
* Example
8+
* Given 7->1->6 + 5->9->2. That is, 617 + 295.
9+
*
10+
* Return 2->1->9. That is 912.
11+
*
12+
* Given 3->1->5 and 5->9->2, return 8->0->8.
13+
*/
14+
15+
#include <iostream>
16+
17+
struct ListNode {
18+
int val;
19+
ListNode *next;
20+
ListNode(int v): val(v), next(nullptr)
21+
{ }
22+
};
23+
24+
ListNode * addLists( ListNode * list1, ListNode *list2 )
25+
{
26+
ListNode *list3 = nullptr;
27+
ListNode *curr = nullptr;
28+
int carry = 0;
29+
while ( list1 && list2 ) {
30+
int x = list1->val + list2->val + carry;
31+
if ( x > 9 ) {
32+
carry = x / 10;
33+
x %= 10;
34+
} else {
35+
carry = 0;
36+
}
37+
if (list3 == nullptr) {
38+
list3 = new ListNode(x);
39+
curr = list3;
40+
} else {
41+
curr->next = new ListNode(x);
42+
curr = curr->next;
43+
}
44+
list1 = list1->next;
45+
list2 = list2->next;
46+
}
47+
48+
while(list1) {
49+
int x = list1->val + carry;
50+
if ( x > 9 ) {
51+
carry = x / 10;
52+
x %= 10;
53+
} else {
54+
carry = 0;
55+
}
56+
curr->next = new ListNode(x);
57+
curr = curr->next;
58+
list1 = list1->next;
59+
}
60+
while(list2) {
61+
int x = list2->val + carry;
62+
if ( x > 9 ) {
63+
carry = x / 10;
64+
x %= 10;
65+
} else {
66+
carry = 0;
67+
}
68+
curr->next = new ListNode(x);
69+
curr = curr->next;
70+
list2 = list2->next;
71+
}
72+
if (carry > 0) {
73+
curr->next = new ListNode(carry);
74+
curr = curr->next;
75+
}
76+
return list3;
77+
}
78+
79+
void iterate(ListNode *node) {
80+
while (node) {
81+
std::cout << node->val << " ";
82+
node = node->next;
83+
}
84+
std::cout << std::endl;
85+
}
86+
87+
int main()
88+
{
89+
ListNode *list1, *list2;
90+
list1 = new ListNode(9);
91+
list1->next = new ListNode(9);
92+
93+
list2 = new ListNode(5);
94+
list2->next = new ListNode(9);
95+
list2->next->next = new ListNode(2);
96+
97+
iterate(list1);
98+
iterate(list2);
99+
ListNode *list3 = addLists(list1, list2);
100+
iterate(list3);
101+
102+
103+
104+
105+
return 0;
106+
107+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Find nth to last node in a linked list.
3+
*
4+
*/
5+
6+
7+
8+
#include<iostream>
9+
10+
11+
struct ListNode {
12+
int val;
13+
ListNode *next;
14+
ListNode(int v): val{v}, next{nullptr} { }
15+
};
16+
17+
ListNode* nthToLastNode( ListNode *head, int n )
18+
{
19+
ListNode *ptr1 = head;
20+
ListNode *ptr2 = head;
21+
22+
int i = 1;
23+
while ( ptr1 && i <= n) {
24+
ptr1 = ptr1->next;
25+
++i;
26+
}
27+
while( ptr1) {
28+
ptr1 = ptr1->next;
29+
ptr2 = ptr2->next;
30+
}
31+
return ptr2;
32+
}
33+
34+
void insert( ListNode* & head, int v )
35+
{
36+
ListNode *newNode = new ListNode(v);
37+
if ( head == nullptr) {
38+
head = newNode;
39+
} else {
40+
ListNode *temp = head;
41+
while( temp->next != nullptr ) {
42+
temp = temp->next;
43+
}
44+
temp->next = newNode;
45+
}
46+
}
47+
48+
void iterateList(ListNode *head) {
49+
while(head) {
50+
std::cout << head->val << "-->";
51+
head = head->next;
52+
}
53+
std::cout << "NULL" << std::endl;
54+
}
55+
56+
int main()
57+
{
58+
ListNode * head = nullptr;
59+
insert(head, 3);
60+
insert(head, 2);
61+
insert(head, 1);
62+
insert(head, 5);
63+
iterateList(head);
64+
65+
ListNode *nthNodeToLastNode = nthToLastNode(head, 2);
66+
std::cout << nthNodeToLastNode->val << std::endl;
67+
68+
}
69+

0 commit comments

Comments
 (0)