Home > Back-end >  List the null pointer
List the null pointer

Time:10-22

Topic is the restructuring of the power button list links: https://leetcode-cn.com/problems/reorder-list/

The code

 
Public ListNode sumIsN (ListNode head) {

If (head==null) return the head;
If (head. Next==null) return the head;

//list of temporary variable head new list before the end of the chain end staging the head pointer
ListNode front, nextTail, tail, newtail nextHead;


The front=newtail=head;//pointer initialization
NextTail tail of==null;//initialize


Do {
NextTail=front;//tail at the beginning of the positioning
NextHead=front. Next;//the next head

//1. Traversing the list before looking for tail and tail chain
While (nextTail. Next. Next! NextTail==null) nextTail. Next;
Tail=nextTail. Next;

//2, the new list even the head end is connected to the current chain tail set next to null
Newtail. Next=front;
Front. Next=tail;
NextTail. Next=null;

//3. Update front and newtail
Newtail=tail;
The front=nextHead;

} while (nextHead!=nextTail & amp; & NextHead. Next!=nextTail);
//total number of odd head equals the tail pointer
//even head tail to

//the last time the connection
Newtail. Next=front;

Return the head;
}



When faced with 1 - & gt; 2 when the input error. The Java lang. NullPointerException
Points to the while (nextTail. Next. Next! NextTail==null) nextTail. Next;
Just enter the dowhile nextTail debug found on next, next to empty
But don't know how to modify... The couple consult



CodePudding user response:

Give you a sample reference

 public class Sample {
Static class ListNode {
ListNode next;
int value;
Public ListNode () {}
Public ListNode (int value) {this.value=https://bbs.csdn.net/topics/value; }
Public void print () {
System. The out. Printf (" % d ", value);
ListNode node=this. Next;
While (node!=null) {
System. The out. Printf (" - & gt; % d ", the node. The value);
The node=node. The next;
}
}
}
Public static ListNode reorderList (ListNode head) {
If (head==null) return the head;
If (head. Next==null) return the head;
ListNode node=head;
ListNode pre=head;
ListNode next=pre. Next;
Do {
While (next!=null & amp; & Next next!=null) {//find the last node and the last but one node
The pre=pre. Next;
Next=next. Next;
}
If (next!=null) {//if you can find the last node
Next) next=node) next;//the last node of the next point to the next node node
Node. Next=next;//and then the current node node next want to the last node (equivalent to insert the last node into the node and the node between next)
The pre. Next=null;//and then modify the last but one node for the last node
The node=next. Next;//before then change when the node node to node before. The next node
} else {
break;
}
The pre=node;//renew modify pre and next, continue to the next cycle
Next=pre. Next;
} while (node!=null & amp; & Node. Next!=null);
Return the head;
}
Public static void main (String [] args) {
ListNode head=new ListNode (1);
ListNode node=head;
for (int i=1; i<5; I++) {
Node. The next=new ListNode (I + 1);
The node=node. The next;
}
Head. The print ();
System.out.println();
The head=reorderList (head);
Head. The print ();
}
}
  • Related