leetcode_92_Reverse Linked List II

描述:

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->NULL, m = 2 and n =
4,

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

Note:

Given m, n satisfy the following condition:

1 ≤ m ≤ n ≤ length of list.

思路:

这种题目,举个例子能让思路更加清晰,通过在草纸上演算可知,题目要分两种情况,m==1和m>1的情况,然后就是围绕这两种情况展开讨论,删除后面的结点,然后将后面的结点添加到前面,一次搞定,bravo!

代码:

public ListNode reverseBetween(ListNode head, int m, int n) {
		if (head==null) {
			return null;
		}
        ListNode p =head,q=head,temp=null;
        int i=0;
        for(i=0;i<m-1;i++)
        	q=q.next;
        for(i=0;i<m-2;i++)
        	p=p.next;
        if(m==1)
        {
        	for(i=0;i<n-m;i++)
            {
            	//delete the node in the list
            	temp=q.next;
            	q.next=temp.next;
            	//insert the node in the list
            	temp.next=p;
            	p=temp;
            }
        	head=p;
        }else
        {
        	for(i=0;i<n-m;i++)
            {
            	//delete the node in the list
            	temp=q.next;
            	q.next=temp.next;
            	//insert the node in the list
            	temp.next=p.next;
            	p.next=temp;
            }
		}
		return head;
    }

结果:

时间: 2024-11-05 13:32:06

leetcode_92_Reverse Linked List II的相关文章

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

Reverse Linked List II

题目 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II 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->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

leetcode - Reverse Linked List II

leetcode - Reverse Linked List II 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the fol

14. Reverse Linked List II

Reverse Linked List II 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following co

Reverse Linked List II leetcode java

题目: 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

LeetCode OJ - Reverse Linked List II

题目: 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ l

[LeetCode][JavaScript]Reverse Linked List II

Reverse Linked List II 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following cond

【LeetCode】Reverse Linked List II

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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt