
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Plus One Linked List in C++
Suppose we have a non-negative integer represented as non-empty a singly linked list of digits, now we have to plus one to the integer. We may assume the integer do not contain any leading zero, except the number 0 itself. In the linked list the most significant digit is at the head of the list.
So, if the input is like [1,2,3], then the output will be [1,2,4]
To solve this, we will follow these steps −
if head is null, then −
return head
curr = head
req = NULL
while curr is non-zero, do −
if val of curr is not equal to 9, then −
req := curr
curr := next of curr
if not req is non-zero, then −
dummy = new node with value 1
next of dummy := head
while head is non-zero, do −
val of head := 0
head := next of head
return dummy
Otherwise
increase val of req by 1
req := next of req
while req is non-zero, do −
val of req := 0
req := next of req
return head
Example
Let us see the following implementation to get a better understanding.
#include <bits/stdc++.h> using namespace std; class ListNode{ public: int val; ListNode *next; ListNode(int data){ val = data; next = NULL; } }; ListNode *make_list(vector<int< v){ ListNode *head = new ListNode(v[0]); for(int i = 1; i<v.size(); i++){ ListNode *ptr = head; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = new ListNode(v[i]); } return head; } void print_list(ListNode *head){ ListNode *ptr = head; cout << "["; while(ptr){ cout << ptr->val << ", "; ptr = ptr->next; } cout << "]" << endl; } class Solution { public: ListNode* plusOne(ListNode* head) { if (!head) return head; ListNode* curr = head; ListNode* req = NULL; while (curr) { if (curr->val != 9) { req = curr; } curr = curr->next; } if (!req) { ListNode* dummy = new ListNode(1); dummy->next = head; while (head) { head->val = 0; head = head->next; } return dummy; } else { req->val++; req = req->next; while (req) { req->val = 0; req = req->next; } return head; } } }; main(){ Solution ob; vector<int< v = {1,4,5}; ListNode *head = make_list(v); print_list(ob.plusOne(head)); }
Input
{1,4,5}
Output
[1, 4, 6, ]
- Related Articles
- Plus One in Python
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- Check if a linked list is Circular Linked List in C++
- Difference between Singly linked list and Doubly linked list in Java
- Program to find linked list intersection from two linked list in Python
- What are the specifications of one plus 5?
- Merge a linked list into another linked list at alternate positions in Java
- Palindrome Linked List in Python
- Linked List Jumps in C++
- Linked List representation in Javascript
- Reverse Linked List in Python
- Linked List Cycle in Python
- Linked List Components in C++
- What are the specifications of one plus 3T phone?
