描述:
(1)翻转一个链表
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
**************************************************************************
(2)将两个排序链表合并为一个新的排序链表
样例
给出 1->3->8->11->15->null
,2->null
, 返回 1->2->3->8->11->15->null
。
解题思路:
已经定义好ListNode链表类,翻转可以采用栈(stack)来进行读取,也可以采用三个指针来变换,还可以采用递归的方式。
排序就要用到两个指针,头指针和尾指针,尾指针进行数据的指引操作,最后返回头指针即可,也可以采用递归的方式。
代码如下:
import java.util.Stack; public class 翻转链表 { /** * Definition for ListNode. */ public class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } /* * @param head: n * * @return: The new head of reversed linked list. */ public ListNode reverse(ListNode head) { // write your code here if (head == null) return null; ListNode reverseHead = new ListNode(0); ListNode current = reverseHead; Stack<ListNode> st = new Stack<ListNode>();//定义栈 while (head != null) { st.push(head); //压入栈中 head = head.next; } while (!st.empty()) { current.next = st.pop(); //取出节点 current = current.next; } current.next = null; return reverseHead.next; } /** * @param head: The head of linked list. * @return: The new head of reversed linked list. */ public ListNode reverse2(ListNode head) { // write your code here ListNode revHead = null; ListNode prev = null; while(head!=null){ ListNode pNext = head.next; if(pNext == null){ // 翻转后的头节点,后面是空,结果 revHead = head; } // head的下一个节点指向之前要链接的节点 head.next = prev; // 要链接的节点 直线p节点,以供下次链接 prev = head; // head节点更新,指向pNext head = pNext; } return revHead; } /** * 递归 * @param head * @return */ public ListNode reverse3(ListNode head) { // write your code here if( head ==null || head.next ==null) return head; ListNode second = head.next; head.next = null; ListNode res = reverse(second); second.next = head; return res; } }
public class 合并两个排序链表 { /** * Definition for ListNode. */ public class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } /** * @param ListNode * l1 is the head of the linked list * @param ListNode * l2 is the head of the linked list * @return: ListNode head of linked list */ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { // write your code here if (l1 == null && l2 != null) return l2; if (l1 != null && l2 == null) return l1; if (l1 == null && l2 == null) return null; ListNode head = new ListNode(0); //头指针 ListNode current = head; //尾指针 while (l1 != null && l2 != null) { if (l1.val <= l2.val) { current.next = l1; current = current.next; l1 = l1.next; } else { current.next = l2; current = current.next; l2 = l2.next; } } if (l1 != null) current.next = l1; if (l2 != null) current.next = l2; return head.next; } /** * 递归方法 * @param l1 * @param l2 * @return */ public ListNode mergeTwoLists2(ListNode l1, ListNode l2) { // write your code here if(l1==null && l2!=null) return l2; if(l1!=null && l2==null) return l1; if(l1==null && l2==null) return null; ListNode MergeHead = null; if(l1.val < l2.val){ MergeHead = l1; MergeHead.next = mergeTwoLists(l1.next,l2); }else{ MergeHead = l2; MergeHead.next = mergeTwoLists(l1,l2.next); } return MergeHead; } }