将字符串转换为单链表

huangapple go评论103阅读模式
英文:

String to a singly linked list

问题

我正在尝试将一个字符串转换为链表,其中每个数字都在单独的节点中。

我着手尝试调试它,但似乎找不到我的逻辑有什么问题。
我一直得到一个奇怪的两位数,而不一定是字符串中出现的数字。

请注意,ListNode 是我创建新节点对象的类。

  1. String number = "807";
  2. int size = number.length();
  3. int pos = 0;
  4. ListNode dummyhead = new ListNode();
  5. ListNode curr = dummyhead;
  6. while (size > 0){
  7. curr.next = new ListNode(number.charAt(pos));
  8. pos++;
  9. size--;
  10. curr = curr.next;
  11. }
  12. return dummyhead.next;
英文:

I am trying to convert a string to a linked list where each digit is in a separate node.

I set on it trying to debug it but I can't seem to find what's wrong with my logic.
I keep getting a weird 2-digit number in each node and not necessarily even the digits that appear in the string.

Please note the ListNode is the class I create a new node object with.

  1. String number = "807";
  2. int size = number.length();
  3. int pos = 0;
  4. ListNode dummyhead = new ListNode();
  5. ListNode curr = dummyhead;
  6. while (size > 0){
  7. curr.next = new ListNode(number.charAt(pos));
  8. pos++;
  9. size--;
  10. curr = curr.next;
  11. }
  12. return dummyhead.next;

答案1

得分: 2

I think you're on the right track. The method works fine, but it seems you are not iterating the list right. Here is how I tested your code:

  1. public class ListNode {
  2. ListNode next;
  3. char data;
  4. public ListNode(char data) {
  5. this.data = data;
  6. }
  7. public ListNode() {}
  8. }
  1. private static ListNode getList(String number) {
  2. int size = number.length();
  3. int pos = 0;
  4. ListNode dummyhead = new ListNode();
  5. ListNode curr = dummyhead;
  6. while (size > 0) {
  7. curr.next = new ListNode(number.charAt(pos));
  8. pos++;
  9. size--;
  10. curr = curr.next;
  11. }
  12. return dummyhead.next;
  13. }
  14. private static String printList(ListNode head) {
  15. ListNode n = head;
  16. StringBuilder sb = new StringBuilder();
  17. while (n != null) {
  18. sb.append(n.data + "-");
  19. n = n.next;
  20. }
  21. return sb.toString();
  22. }
  23. public static void main(String[] args) {
  24. String number = "807";
  25. System.out.println(printList(getList(number)));
  26. }

Output:

  1. 8-0-7-
英文:

I think you're on the right track. The method works fine, but it seems you are not iterating the list right. Here is how I tested your code:

  1. public class ListNode{
  2. ListNode next;
  3. char data;
  4. public ListNode(char data) {
  5. this.data = data;
  6. }
  7. public ListNode() {}
  8. }
  1. private static ListNode getList(String number){
  2. int size = number.length();
  3. int pos = 0;
  4. ListNode dummyhead = new ListNode();
  5. ListNode curr = dummyhead;
  6. while (size > 0){
  7. curr.next = new ListNode(number.charAt(pos));
  8. pos++;
  9. size--;
  10. curr = curr.next;
  11. }
  12. return dummyhead.next;
  13. }
  14. private static String printList(ListNode head) {
  15. ListNode n = head;
  16. StringBuilder sb = new StringBuilder();
  17. while(n != null) {
  18. sb.append(n.data+"-");
  19. n = n.next;
  20. }
  21. return sb.toString();
  22. }
  23. public static void main(String[] args) {
  24. String number = "807";
  25. System.out.println(printList(getList(number)));
  26. }

Output:

  1. 8-0-7-

答案2

得分: 1

"charAt" 返回指定位置的字符。char 是一种数字类型,但它表示它所代表的字符的ASCII地址。

  1. "807".charAt(0)

返回 56,因为这是 8 的ASCII值。

我怀疑您在 ListNode 内部有一个保存 char 到常规 intint data 字段。

因此,您的 "807" 将被转换为数字 56、48、55 的列表。

显然,您希望将 ""8"" 保存到节点中,所以可以使用以下方式:

  1. while (size > 0){
  2. //对于 pos=0,这将字符串 ""8"" 转换为整数 8:
  3. Integer n = Integer.valueOf(number.substring(pos, pos+1));
  4. curr.next = new ListNode(n);

或者,如Majed在他的答案中建议的那样,将 ListNode 内部的 data 字段类型更改为 char

英文:

charAt returns the character of the specified position. A char is a numeric type, but it represents the ascii address of the character it represents.

  1. "807".charAt(0)

returns 56 because that is the ascii value of 8.

I suspect that you have a int data field inside ListNode, which saves that char into a regular int.

Your "807" will thus be transformed into a list of the numbers 56, 48, 55.

You apparently want to save that "8" into the node, so use

  1. while (size > 0){
  2. //for pos=0, this converts the string "8" to the integer 8:
  3. Integer n = Integer.valueOf(number.substring(pos, pos+1));
  4. curr.next = new ListNode(n);

Or, as Majed suggests in his answer, change the type of your data field inside of ListNode to char.

huangapple
  • 本文由 发表于 2020年8月14日 04:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63403068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定