This week’s LeetCode problem is 206. Reverse Linked List

Description: Given the head of a singly linked list, reverse the list, and return the reversed list. For example, `head = [1,2,3,4,5]`

, the result is `[5,4,3,2,1]`

.

Solution Idea: original linked list is `prev->curr`

, after reversing, it will become `curr->prev`

.

Accepted Code

`class Solution {`

public ListNode reverseList(ListNode head) {

ListNode prev = null;

ListNode curr = head;

while (curr != null) {

ListNode next = curr.next;

curr.next = prev;

prev = curr;

curr = next;

}

return prev;

}

}

This week’s Review is for the following article: GIT PURR! …

This week’s LeetCode problem is 8. String to Integer (atoi).

Description: Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++’s atoi function).

Solution Idea: Because `int`

may have overflow, you can use `long`

variable to store the result first and then determine whether there is overflow. The string matching rules are as follows.

- (optional) It can be skipped automatically if start with spaces(
- (Optional)
`+/-`

can be used to record whether the number is positive or negative. - (mandatory)
`isDigit(ch) == true`

is the number One thing to note is. - (Must be…

This week’s LeetCode problem is 1. Two Sum

Introduction

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.

For example: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Solution Idea

The idea of the problem is very simple, traverse the array, find whether `target - nums[0]`

is in the array nums, if in return…

This week’s LeetCode problem is 189. Rotating Arrays

Problem Description

Given an array, rotate the array to the right by k steps, where k is non-negative. The example is as follows.

`Input array [1,2,3,4,5,6,7], k = 3`

Output array: [5,6,7,1,2,3,4]

Explanation:

Rotate 1 steps to the right: [7,1,2,3,4,5,6]

Rotate 2 steps to the right: [6,7,1,2,3,4,5]

Rotate 3 steps to the right: [5,6,7,1,2,3,4]

Solution 1: My own idea

Create a new array to store the result. Because I found that `now nums[i] = original nums[i + nums.length - k % nums.length]`

after rotating and moving. Taking the modulu because when `k`

…