GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/src/ReversedLinkedListII.java at master · LeBW/leetcode Expand file tree/ Copy pathReversedLinkedListII.javaMore file actions 40 lines (39 loc) · 1.14 KB / Copy pathReversedLinkedListII.javaFile metadata and controls40 lines (39 loc) · 1.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 /** * 92. Reversed LinkedList II * 链表翻转 * @author LBW */ public class ReversedLinkedListII { public ListNode reverseBetween(ListNode head, int left, int right) { // 使用虚拟头节点避免分类讨论 ListNode dummy = new ListNode(-1); dummy.next = head; // 找到 left 前的节点和 right 后的节点 ListNode leftNode = dummy; for (int i = 0; i < left - 1; i++) { leftNode = leftNode.next; } ListNode cur = leftNode; for (int i = 0; i < right - left + 1; i++) { cur = cur.next; } // 切断,进行翻转 ListNode rightNode = cur.next; cur.next = null; ListNode reverseHead = reverse(leftNode.next); // 连接 leftNode.next.next = rightNode; leftNode.next = reverseHead; return dummy.next; } private ListNode reverse(ListNode head) { ListNode pre = null, cur = head, next = head.next; while (cur != null) { next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |