-
Notifications
You must be signed in to change notification settings - Fork 7
369. Plus One Linked List #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Another sentinel node question. This actually goes together with this. Perhaps as a preparation question for the question 426. |
|
Why can't I merge this PR? |
There is a conflict with the code in master. |
|
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. |
Yep. We can assume that the Turing machine is the basis of all modern computers. |

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 bep. In such a situation with 9-9-9-9, this nodepwill obtain the value 1.At the end, we return node
pif its value is not 1, else, we return the head.