Leetcode#79Word Search

Reverse Linked List II

Total Accepted: 39279 Total Submissions: 150148My Submissions

Question Solution

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

分析,顺序扫描链表,找出需要旋转的片段,旋转,关键点测试样例,全局旋转和部分旋转

public class Solution {

public ListNode reverseBetween(ListNode head, int m, int n) {

if(head==null)

return head;

else if(head.next==null)

return head;

else if(m<1)

m=1;

else if(m>=n)

return head;

ListNode x=new ListNode(0);

x.next=head;

ListNode y=x;

int i=1;

while(i<m)

{

y=y.next;

i++;

}

if(y.next==null)

return head;

else

{

int j=0;

ListNode e=y.next;

ListNode z=y.next;

ListNode v=z.next;

while(j<n-m+1&&v!=null)

{

z.next=y.next;

y.next=z;

z=v;

v=z.next;

j++;

}

if(j==n-m+1)

e.next=z;

else

{

z.next=y.next;

y.next=z;

e.next=v;

}

return x.next;

}

}

}

时间: 2024-08-01 11:15:42

Leetcode#79Word Search的相关文章

[LeetCode] 034. Search for a Range (Medium) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search for a Range (Medium) 链接: 题目:https://leetcode.com/problems/search-for-a-range/ 代码(github):https://github.com/illuz/leetcode 题意: 在有序数组中找到一个数的范围.(由于数

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

[LeetCode] 035. Search Insert Position (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一

【Leetcode】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

[LeetCode] Word Search [37]

题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be

LeetCode: Word Search [079]

[题目] Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not

【LeetCode】- Search Insert Position(查找插入的位置)

[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

Java for LeetCode 081 Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 参考Java for LeetCode 033 Search in Rota

LeetCode Solutions : Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Java Solution ( refer to my blog LeetCode So