Reverse Linked List I,II

Reverse a singly linked list.

题意:反转链表

方法一:遍历

思路:使用辅助节点dummy。初始化时dummy..next指向head,并从第二个节点开始遍历,

把遍历过的节点一次插入在dummy节点之后,最后返回dummy.next即可

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) {
			return null;
		}
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        //从第二个节点开始遍历
        ListNode curNode = head.next;
        //链表尾节点的next设为null
        head.next = null;
        while (curNode != null) {
        	//依次插入遍历后的节点
        	ListNode next = curNode.next;//用来记录curNode的下一个节点的地址
        	curNode.next = dummy.next;
        	dummy.next = curNode;
        	curNode = next;
        	
        
		}
        return dummy.next;
    }
}

方法二:递归

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) {
			return null;
		}
		if (head.next == null) {
			return head;
		}
		ListNode preNode = head.next;
		ListNode n = reverseList(preNode);
		head.next = null;
		preNode.next = head;
		return n;
    }
}

第二种方法递归:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) {
			return null;
		}
		if (head.next == null) {
			return head;//递归结束条件判断
		}
		ListNode preNode = head.next;
		ListNode n = reverseList(preNode);
		head.next = null;
		preNode.next = head;//反转指针方向
		return n;//返回新链表表头
    }
}

 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->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.

从第m个到第n个元素反转链表

为了更好的处理表头和第M个节点的关系,引入dummy节点,此外,还需记录第m-1个节点。从第m个节点开始遍历至第n个节点,已经将遍历过的节点插入在第m-1个节点之后,并保证第m个节点的next指向遍历节点的next,以避免链表断裂。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if (head == null) return null;
		ListNode dummy = new ListNode(0);
		dummy.next = head;
		ListNode curNode = head;
		//m的前一个元素
		ListNode preNode = dummy;
		//m节点
		ListNode mNode = new ListNode(0);
		ListNode nextNode = null;
		
		for (int i = 1; i <= n; i++) {
			//记录第m个节点
			if (i == m) mNode = curNode;
			//记录m-1个节点
			if (i < m ) preNode = preNode.next;
			nextNode = curNode.next;
			if (i > m && i <= n) {
				mNode.next = nextNode;//避免链表断裂
				curNode.next = preNode.next;
				preNode.next = curNode;
			}
			curNode = nextNode;
		}
      
        
        return dummy.next;
  
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值