将字符串转换为单链表

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

String to a singly linked list

问题

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

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

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

String number = "807";
   
int size = number.length();
int pos = 0;
ListNode dummyhead = new ListNode();
ListNode curr = dummyhead;
while (size > 0){
    curr.next = new ListNode(number.charAt(pos));   
    pos++;
    size--;
    curr = curr.next;
}

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.

String number = "807";
   
int size = number.length();
int pos = 0;
ListNode dummyhead = new ListNode();
ListNode curr = dummyhead;
while (size > 0){
    curr.next = new ListNode(number.charAt(pos));   
    pos++;
    size--;
    curr = curr.next;
}

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:

public class ListNode {
    ListNode next;
    char data;
    public ListNode(char data) {
        this.data = data;
    }
    public ListNode() {}
}
private static ListNode getList(String number) {
    int size = number.length();
    int pos = 0;
    ListNode dummyhead = new ListNode();
    ListNode curr = dummyhead;
    while (size > 0) {
        curr.next = new ListNode(number.charAt(pos));   
        pos++;
        size--;
        curr = curr.next;
    }
    return dummyhead.next;
}

private static String printList(ListNode head) {
    ListNode n = head;
    StringBuilder sb = new StringBuilder();
    while (n != null) {
        sb.append(n.data + "-");
        n = n.next;
    }
    return sb.toString();
}

public static void main(String[] args) {
    String number = "807";
    System.out.println(printList(getList(number)));
}

Output:

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:

public class ListNode{
	ListNode next;
	char data;
	public ListNode(char data) {
		this.data = data;
	}
	public ListNode() {}
}
private static ListNode getList(String number){
	int size = number.length();
    int pos = 0;
    ListNode dummyhead = new ListNode();
    ListNode curr = dummyhead;
    while (size > 0){
        curr.next = new ListNode(number.charAt(pos));   
        pos++;
        size--;
        curr = curr.next;
   }
   return dummyhead.next;
}
private static String printList(ListNode head) {
	ListNode n = head;
	StringBuilder sb = new StringBuilder();
	while(n != null) {
		sb.append(n.data+"-");
		n = n.next;
	}
	return sb.toString();
}
public static void main(String[] args) {
	String number = "807";
	System.out.println(printList(getList(number)));
}

Output:

8-0-7-

答案2

得分: 1

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

"807".charAt(0)

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

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

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

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

while (size > 0){
  //对于 pos=0,这将字符串 ""8"" 转换为整数 8:
  Integer n = Integer.valueOf(number.substring(pos, pos+1));
  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.

"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

while (size > 0){
  //for pos=0, this converts the string "8" to the integer 8:
  Integer n = Integer.valueOf(number.substring(pos, pos+1));
  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:

确定