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;
  
    }
}


發佈了97 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章