369. Plus One Linked List
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method Explanation
The problem asks us to add one to a number represented by a singly linked list. The most significant digit is at the head. A key challenge is that addition starts from the least significant digit (the tail of the list), but a singly linked list only allows forward traversal.
Solution
Plus One Linked List in Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def plusOne(self, head: ListNode) -> ListNode:
# Sentinel node to handle cases like 9, 99, 999...
sentinel = ListNode(0)
sentinel.next = head
# This pointer will track the rightmost digit that is not 9.
not_nine = sentinel
# Traverse the list to find the rightmost not-9 digit.
curr = head
while curr:
if curr.val != 9:
not_nine = curr
curr = curr.next
# Increment the rightmost not-9 digit by one.
not_nine.val += 1
# All digits after not_nine were 9s. Change them to 0s.
curr = not_nine.next
while curr:
curr.val = 0
curr = curr.next
# If sentinel's value is 1, it's the new head. Otherwise, return the original head.
if sentinel.val == 1:
return sentinel
else:
return sentinel.next
Plus One Linked List in Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode plusOne(ListNode head) {
// Sentinel node to handle cases like 9, 99, 999...
ListNode sentinel = new ListNode(0);
sentinel.next = head;
// This pointer will track the rightmost digit that is not 9.
ListNode notNine = sentinel;
// Traverse the list to find the rightmost not-9 digit.
ListNode curr = head;
while (curr != null) {
if (curr.val != 9) {
notNine = curr;
}
curr = curr.next;
}
// Increment the rightmost not-9 digit by one.
notNine.val++;
// All digits after notNine were 9s. Change them to 0s.
curr = notNine.next;
while (curr != null) {
curr.val = 0;
curr = curr.next;
}
// If sentinel's value is 1, it's the new head. Otherwise, return the original head.
// Java's garbage collector will handle the unused sentinel object.
return sentinel.val == 1 ? sentinel : sentinel.next;
}
}
Plus One Linked List in C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* plusOne(ListNode* head) {
// Sentinel node to handle cases like 9, 99, 999...
ListNode* sentinel = new ListNode(0);
sentinel->next = head;
// This pointer will track the rightmost digit that is not 9.
ListNode* not_nine = sentinel;
// Traverse the list to find the rightmost not-9 digit.
ListNode* curr = head;
while (curr != nullptr) {
if (curr->val != 9) {
not_nine = curr;
}
curr = curr->next;
}
// Increment the rightmost not-9 digit by one.
not_nine->val += 1;
// All digits after not_nine were 9s. Change them to 0s.
curr = not_nine->next;
while (curr != nullptr) {
curr->val = 0;
curr = curr->next;
}
// If sentinel's value is 0, it means no new leading digit was needed.
// The original head (or its modified version) is the answer.
// If sentinel's value is 1, it means the number was all 9s and we have a new head.
if (sentinel->val == 0) {
ListNode* result = sentinel->next;
delete sentinel; // Clean up the sentinel node as it's not part of the result list.
return result;
}
return sentinel;
}
};
Plus One Linked List in TypeScript
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function plusOne(head: ListNode): ListNode {
// Sentinel node to handle cases like 9, 99, 999...
const sentinel = new ListNode(0, head);
// This pointer will track the rightmost digit that is not 9.
let notNine: ListNode = sentinel;
// Traverse the list to find the rightmost not-9 digit.
let curr: ListNode | null = head;
while (curr) {
if (curr.val !== 9) {
notNine = curr;
}
curr = curr.next;
}
// Increment the rightmost not-9 digit by one.
notNine.val++;
// All digits after notNine were 9s. Change them to 0s.
curr = notNine.next;
while (curr) {
curr.val = 0;
curr = curr.next;
}
// If sentinel's value is 1, it's the new head. Otherwise, return the original head.
// Since constraints state list is non-empty, sentinel.next will not be null.
return sentinel.val === 1 ? sentinel : sentinel.next!;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.