Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented May 8, 2021

Issue.
Let us assume that we are given the head of list like 1-2-3. The operation on this is pretty easy. We just add 1 to the last node, the node without a next node.
What if this number was something like 1-9? Then this operation wouldn't have worked out, as the result would have been 1-10. Hence, we should increment the last digit that is not 9 by 1, then set all the values thereafter to 0. Result is then 2-0. This algorithm also works out for 1-9-9-9, 1-0-9, 8-9-9 and such.
The last possibility is that the given list only contains 9's. Then we should have a some sort of placeholder node p, whose next node will be our start node. If our head node has the value 9, then the last nine node should be p. In such a situation with 9-9-9-9, this node p will obtain the value 1.
At the end, we return node p if its value is not 1, else, we return the head.

@ErdemT09 ErdemT09 requested a review from altay9 May 8, 2021 14:08
@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented May 8, 2021

Note: In these questions with nodes, graphs and such, I am strongly seeing some aspects of Turing Machines. I am not really well-versed in this topic but that are my observations.


public class PlusOneLinkedList {

public ListNode addOne(ListNode head)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the method name to "plusOne" in order to be compatible with LeetCode?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a smart solution using sentinel node as a dummy head.
image

@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented May 8, 2021

Another sentinel node question. This actually goes together with this. Perhaps as a preparation question for the question 426.

@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented May 8, 2021

Why can't I merge this PR?

@altay9
Copy link
Collaborator

altay9 commented May 8, 2021

Why can't I merge this PR?

There is a conflict with the code in master.
Can you try to pull master and merge it with the master.
Then push again, and let us see what will happen.

@altay9 altay9 merged commit 7a759a6 into master May 8, 2021
@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented May 8, 2021

I guess you have used Git commands to merge it. I did not understand what the conflict here was, but I guess the solution you have mentioned would also have worked.

@ErdemT09 ErdemT09 deleted the 369.-Plus-One-Linked-List branch May 8, 2021 15:56
@altay9
Copy link
Collaborator

altay9 commented May 8, 2021

Note: In these questions with nodes, graphs and such, I am strongly seeing some aspects of Turing Machines. I am not really well-versed in this topic but that are my observations.

Yep. We can assume that the Turing machine is the basis of all modern computers.
Perhaps it is possible to think of a Turing machine as a binary decision tree that is constructed as a boolean array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants