use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.
the class _369 method plusOne.
public ListNode plusOne(ListNode head) {
// get the length of the list and take out the value of each node and store them into an array
ListNode temp = head;
int len = 0;
while (temp != null) {
len++;
temp = temp.next;
}
int[] nums = new int[len];
temp = head;
int j = 0;
while (temp != null) {
nums[j++] = temp.val;
temp = temp.next;
}
// plus one into this array: nums
for (int i = len - 1; i >= 0; i--) {
if (nums[i] != 9) {
nums[i]++;
break;
} else {
nums[i] = 0;
}
}
// still assuming the first value in the list should not be zero as it's representing a valid number, although it's in a list
ListNode pre = new ListNode(-1);
if (nums[0] == 0) {
// in this case, let's just construct a new linked list and return: only first node value is 1, all the rest is 0
ListNode newHead = new ListNode(1);
ListNode result = newHead;
int count = 0;
while (count++ < len) {
newHead.next = new ListNode(0);
newHead = newHead.next;
}
return result;
} else {
pre.next = head;
for (int i = 0; i < len; i++) {
head.val = nums[i];
head = head.next;
}
return pre.next;
}
}
use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.
the class _445 method addTwoNumbers.
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Deque<Integer> stack1 = popIntoStack(l1);
Deque<Integer> stack2 = popIntoStack(l2);
int sum = 0;
ListNode list = new ListNode(0);
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (!stack1.isEmpty()) {
sum += stack1.removeFirst();
}
if (!stack2.isEmpty()) {
sum += stack2.removeFirst();
}
list.val = sum % 10;
ListNode head = new ListNode(sum / 10);
head.next = list;
list = head;
sum /= 10;
}
return list.val == 0 ? list.next : list;
}
use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.
the class LinkedListUtils method contructLinkedList.
public static ListNode contructLinkedList(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
ListNode pre = new ListNode(-1);
ListNode head = new ListNode(nums[0]);
pre.next = head;
for (int i = 1; i < nums.length; i++) {
head.next = new ListNode(nums[i]);
head = head.next;
}
return pre.next;
}
use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.
the class _143 method reorderList.
public void reorderList(ListNode head) {
if (head == null || head.next == null) {
return;
}
/* first we use two pointers to separate this list into two parts */
ListNode slowNode = head;
ListNode fastNode = head;
while (fastNode.next != null) {
fastNode = fastNode.next;
if (fastNode.next != null) {
fastNode = fastNode.next;
} else {
break;
}
slowNode = slowNode.next;
}
// two sublist heads
ListNode head1 = head;
ListNode head2 = slowNode.next;
// detach the two sublists;
slowNode.next = null;
// reverse the second sublist
ListNode cur = head2;
ListNode post = cur.next;
cur.next = null;
while (post != null) {
ListNode temp = post.next;
post.next = cur;
cur = post;
post = temp;
}
// the new head of the reversed sublist
head2 = cur;
// merge the two sublists as required
ListNode p = head1;
ListNode q = head2;
while (q != null) {
ListNode temp1 = p.next;
ListNode temp2 = q.next;
p.next = q;
q.next = temp1;
p = temp1;
q = temp2;
}
}
use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.
the class _147 method insertionSortList.
public ListNode insertionSortList(ListNode head) {
ListNode temp = head;
List<Integer> list = new ArrayList<Integer>();
while (temp != null) {
list.add(temp.val);
temp = temp.next;
}
Integer[] nums = list.toArray(new Integer[list.size()]);
for (int i = 1; i < list.size(); i++) {
for (int j = i - 1; j >= 0; j--) {
if (nums[j] > nums[j + 1]) {
int tempNum = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tempNum;
}
}
}
ListNode newHead = head;
for (int i = 0; i < nums.length; i++) {
newHead.val = nums[i];
newHead = newHead.next;
}
return head;
}
Aggregations